Scala 中 Seq 和 List 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10866639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between a Seq and a List in Scala
提问by opensas
I've seen in many examples that sometimes a Seq is being used, while other times is the List...
我在很多例子中看到,有时使用 Seq,而有时使用 List...
Is there any difference, other than the former one being a Scala type and the List coming from Java?
除了前者是 Scala 类型和来自 Java 的 List 之外,有什么区别吗?
回答by Daniel C. Sobral
In Java terms, Scala's Seq
would be Java's List
, and Scala's List
would be Java's LinkedList
.
在 Java 术语中,Scala 的Seq
将是 Java 的List
,Scala 的List
将是 Java 的LinkedList
.
Note that Seq
is a trait
, which is equivalent to Java's interface
, but with the equivalent of up-and-coming defender methods. Scala's List
is an abstract class that is extended by Nil
and ::
, which are the concrete implementations of List
.
请注意,Seq
是trait
,它等同于 Java 的interface
,但等同于新兴的防御者方法。ScalaList
是一个抽象类,由Nil
和扩展::
,它们是 的具体实现List
。
So, where Java's List
is an interface
, Scala's List
is an implementation.
所以,JavaList
是一个interface
,ScalaList
是一个实现。
Beyond that, Scala's List
is immutable, which is not the case of LinkedList
. In fact, Java has no equivalent to immutable collections (the read only thing only guarantees the new object cannot be changed, but you still can change the old one, and, therefore, the "read only" one).
除此之外,ScalaList
是不可变的,而LinkedList
. 事实上,Java 没有与不可变集合等价的东西(只读的东西只能保证新对象不能改变,但你仍然可以改变旧对象,因此,“只读”对象)。
Scala's List
is highly optimized by compiler and libraries, and it's a fundamental data type in functional programming. However, it has limitations and it's inadequate for parallel programming. These days, Vector
is a better choice than List
, but habit is hard to break.
ScalaList
通过编译器和库进行了高度优化,它是函数式编程中的基本数据类型。但是,它有局限性,不适合并行编程。这些天,Vector
是比 更好的选择List
,但习惯很难打破。
Seq
is a good generalization for sequences, so if you program to interfaces, you should use that. Note that there are actually three of them: collection.Seq
, collection.mutable.Seq
and collection.immutable.Seq
, and it is the latter one that is the "default" imported into scope.
Seq
是序列的一个很好的概括,所以如果你编程接口,你应该使用它。请注意,实际上有 3 个:collection.Seq
、collection.mutable.Seq
和collection.immutable.Seq
,而后者是导入作用域的“默认值”。
There's also GenSeq
and ParSeq
. The latter methods run in parallel where possible, while the former is parent to both Seq
and ParSeq
, being a suitable generalization for when parallelism of a code doesn't matter. They are both relatively newly introduced, so people doesn't use them much yet.
还有GenSeq
和ParSeq
。后面的方法在平行尽可能运行,而前者是父既Seq
和ParSeq
,是用于当的代码并行无所谓合适的概括。它们都是相对较新推出的,所以人们还没有多少使用它们。
回答by Ceasar Bautista
A Seqis an Iterable that has a defined order of elements. Sequences provide a method apply()
for indexing, ranging from 0 up to the length of the sequence. Seq has many subclasses including Queue, Range, List, Stack, and LinkedList.
甲SEQ是一个可迭代具有定义的元素的顺序。序列提供了一种apply()
索引方法,范围从 0 到序列的长度。Seq 有许多子类,包括 Queue、Range、List、Stack 和 LinkedList。
A Listis a Seq that is implemented as an immutable linked list. It's best used in cases with last-in first-out (LIFO) access patterns.
一个列表是作为不可改变的链表实现的序列。它最适合用于具有后进先出 (LIFO) 访问模式的情况。
Here is the complete collection class hierarchy from the Scala FAQ:
这是Scala FAQ 中完整的集合类层次结构:
回答by Akavall
Seq
is a trait that List
implements.
Seq
是一个List
实现的特性。
If you define your container as Seq
, you can use any container that implements Seq
trait.
如果将容器定义为Seq
,则可以使用任何实现Seq
trait 的容器。
scala> def sumUp(s: Seq[Int]): Int = { s.sum }
sumUp: (s: Seq[Int])Int
scala> sumUp(List(1,2,3))
res41: Int = 6
scala> sumUp(Vector(1,2,3))
res42: Int = 6
scala> sumUp(Seq(1,2,3))
res44: Int = 6
Note that
注意
scala> val a = Seq(1,2,3)
a: Seq[Int] = List(1, 2, 3)
Is just a short hand for:
只是以下内容的简称:
scala> val a: Seq[Int] = List(1,2,3)
a: Seq[Int] = List(1, 2, 3)
if the container type is not specified, the underlying data structure defaults to List
.
如果未指定容器类型,则底层数据结构默认为List
。
回答by zakelfassi
In Scala, a List inherits from Seq, but implements Product; here is the proper definition of List:
在 Scala 中, List 继承自 Seq,但实现了Product;这是List的正确定义:
sealed abstract class List[+A] extends AbstractSeq[A] with Product with ...
[Note: the actualdefinitionis a tad bit more complex, in order to fit in with and make use of Scala's very powerful collection framework.]
[注:在实际的定义,是一点点与和利用Scala的非常强大的集合框架的复杂一点,为了配合]
回答by Profiterole
As @daniel-c-sobral said, List extends the trait Seq and is an abstract class implemented by scala.collection.immutable.$colon$colon
(or ::
for short), but technicalities aside, mind that most of lists and seqs we use are initialized in the form of Seq(1, 2, 3)
or List(1, 2, 3)
which both return scala.collection.immutable.$colon$colon
, hence one can write:
正如@daniel-c-sobral 所说,List 扩展了 trait Seq 并且是一个由scala.collection.immutable.$colon$colon
(或::
简称)实现的抽象类,但撇开技术性不谈,请注意我们使用的大多数列表和 seq 都是以Seq(1, 2, 3)
或List(1, 2, 3)
都返回的形式初始化的scala.collection.immutable.$colon$colon
,因此可以写:
var x: scala.collection.immutable.$colon$colon[Int] = null
x = Seq(1, 2, 3).asInstanceOf[scala.collection.immutable.$colon$colon[Int]]
x = List(1, 2, 3).asInstanceOf[scala.collection.immutable.$colon$colon[Int]]
As a result, I'd argue than the only thing that matters are the methods you want to expose, for instance to prepend you can use ::
from List that I find redundant with +:
from Seq and I personally stick to Seq by default.
因此,我认为唯一重要的是您要公开的方法,例如::
,在我认为与+:
Seq冗余的 List 之前可以使用,我个人默认情况下坚持使用 Seq。