问题描述
我做的斯卡拉项目,但我相当新的语言,有一个Java的背景。我看到Scala没有ArrayList中,所以我想知道什么Scala的相当于Java的ArrayList的叫了,如果有是Java和Scala版本之间的重要区别。
I am doing a project in Scala, but am fairly new to the language and have a Java background. I see that Scala doesn't have ArrayList, so I am wondering what Scala's equivalent of Java's ArrayList is called, and if there are any important differences between the Java and Scala versions.
编辑:我不是在寻找一个特定的行为,而更像是一种内部重新presentation(存储在数组中的数据,但整个数组是不可见的,只有一部分您使用)。
I'm not looking for a specific behavior so much as an internal representation (data stored in an array, but the whole array isn't visible, only the part you use).
推荐答案
的ArrayList
是一个被广泛误用默认列表
在Java中。它具有当元素添加/删除经常表现糟糕,但工程pretty以及为阵列
的替代品。所以,当你问什么是Scala等价,我能想到的三种不同的实际的问题:
ArrayList
is a widely misused "default" List
in Java. It has terrible performance when elements are added/removed frequently, but works pretty well as a replacement for Array
. So, when you ask what is the equivalent in Scala, I can think of three different actual questions:
- 什么是Scala的默认集合?
- 什么斯卡拉收藏有类似
的ArrayList
特点? - 什么是一个很好的替代品
斯卡拉阵列
?
- What is Scala's default collection?
- What Scala collection has characteristics similar to
ArrayList
? - What's a good replacement for
Array
in Scala?
因此,这里有答案了这些:
So here are the answers for these:
Scala的相当于Java中的列表
接口是 SEQ
。一个更普遍的接口中也存在,这是 GenSeq
- 主要的区别是,一个 GenSeq
可能有操作处理串行或并行,根据实现
Scala's equivalent of Java's List
interface is the Seq
. A more general interface exists as well, which is the GenSeq
-- the main difference being that a GenSeq
may have operations processed serially or in parallel, depending on the implementation.
由于斯卡拉允许程序员使用 SEQ
作为工厂,他们不经常,除非他们关心它定义一个特定的实现麻烦。当他们这样做,他们通常会挑选要么Scala的列表
或矢量
。它们都是不可变的,而矢量
具有良好的索引访问性能。在另一方面,列表
做得非常好它做得很好的操作。
Because Scala allows programmers to use Seq
as a factory, they don't often bother with defining a particular implementation unless they care about it. When they do, they'll usually pick either Scala's List
or Vector
. They are both immutable, and Vector
has good indexed access performance. On the other hand, List
does very well the operations it does well.
这是 scala.collection.mutable.ArrayBuffer
。
好了,好消息是,你可以使用阵列
Scala中!在Java中,阵列
通常避免,因为它与一般的仿制药不兼容。它是共变体集合,而泛型是不变的,它是可变的 - 这使得它的协方差的危险时,它接受原语,其中泛型不这样做,并且它有一个pretty有限集的方法
Well, the good news is, you can just use Array
in Scala! In Java, Array
is often avoided because of its general incompatibility with generics. It is a co-variant collection, whereas generics is invariant, it is mutable -- which makes it's co-variance a danger, it accepts primitives where generics don't, and it has a pretty limited set of methods.
在Scala中,阵列
- 这仍然是相同的阵列
在Java中 - 是不变的,这使大多数问题消失。斯卡拉接受 AnyVal
(图元的等值)作为类型的仿制药,即使它会做自动装箱。并通过丰富了我的图书馆模式,所有 SEQ
方法可用于阵列
。
In Scala, Array
-- which is still the same Array
as in Java -- is invariant, which makes most problems go away. Scala accepts AnyVal
(the equivalent of primitives) as types for its "generics", even though it will do auto-boxing. And through the "enrich my library" pattern, ALL of Seq
methods are available to Array
.
所以,如果你想有一个更强大的阵列
,只使用阵列
。
So, if you want a more powerful Array
, just use an Array
.
适用于所有生产的所有集合默认的方法新的的集合。例如,如果我这样做:
The default methods available to all collections all produce new collections. For example, if I do this:
val ys = xs filter (x => x % 2 == 0)
然后 YS
将是一个的新的的集合,而 XS
仍然会是与此相同命令之前。这是真实的,不管是什么 XS
是:阵列
,列表
等。
Then ys
will be a new collection, while xs
will still be the same as before this command. This is true no matter what xs
was: Array
, List
, etc.
当然,这是有代价的 - 毕竟,你的是的产生一个新的集合。 Scala的不可变的集合是在处理这个成本要好得多,因为他们的永久的,而是取决于执行什么操作。
Naturally, this has a cost -- after all, you are producing a new collection. Scala's immutable collections are much better at handling this cost because they are persistent, but it depends on what operation is executed.
没有收集可以做很多关于过滤
,但列表
对由$生成一个新的集合优异性能p $ ppending一个元件或取出头 - 堆的基本操作,事实上。 矢量
有一堆作业性能好,但如果集合不小,只支付。对于,比如说收藏,百达元素,整体成本可能会超过收益。
No collection can do much about filter
, but a List
has excellent performance on generating a new collection by prepending an element or removing the head -- the basic operations of a stack, as a matter of fact. Vector
has good performance on a bunch of operations, but it only pays if the collection isn't small. For collections of, say, up to a hundred elements, the overall cost might exceed the gains.
所以,实际上你可以添加或删除元素的阵列
,和Scala会产生的新的的阵列
为你,但是当你做,你将支付的完整副本的成本。
So you can actually add or remove elements to an Array
, and Scala will produce a new Array
for you, but you'll pay the cost of a full copy when you do that.
Scala的可变集合添加一些其他的方法。特别是,可以增加或减少大小集合 - 而不会产生一个新的集合 - 贯彻落实<$c$c>Growable和<$c$c>Shrinkable特征。他们不保证良好的性能上的这些操作,虽然,但他们会点你要检查出的集合。
Scala mutable collections add a few other methods. In particular, the collections that can increase or decrease size -- without producing a new collection -- implement the Growable
and Shrinkable
traits. They don't guarantee good performance on these operations, though, but they'll point you to the collections you want to check out.
这篇关于斯卡拉相当于java.util.ArrayList中的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!