Scala相当于java.util.ArrayList

时间:2023-01-21 19:36:16

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.

我正在用Scala做一个项目,但是我对这门语言很陌生,并且有Java背景。我看到Scala没有ArrayList,所以我想知道Scala的什么是Java的ArrayList,以及Java和Scala版本之间是否有什么重要的区别。

EDIT: 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).

编辑:我不是在寻找一个特定的行为,而是在寻找一个内部表示(数据存储在一个数组中,但是整个数组是不可见的,只有你使用的部分)。

4 个解决方案

#1


98  

I can think of 3 more specific questions to address yours:

我可以想出三个更具体的问题来回答你:

  • What is Scala's default collection?
  • Scala的默认集合是什么?
  • What Scala collection has characteristics similar to ArrayList?
  • 什么Scala集合具有与ArrayList相似的特性?
  • What's a good replacement for Array in Scala?
  • Scala中的数组有什么好的替代品?

So here are the answers for these:

这些问题的答案如下:

What is Scala's default collection?

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.

Scala等价于Java的列表接口是Seq。还有一个更通用的接口,即GenSeq——主要的区别在于,GenSeq可以根据实现的不同,以串行方式或并行方式处理操作。

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允许程序员将Seq用作工厂,所以他们通常不需要定义特定的实现,除非他们关心它。当它们这样做时,它们通常会选择Scala的列表或向量。它们都是不可变的,而且Vector具有良好的索引访问性能。另一方面,List做得很好,它做得很好。

What Scala collection has characteristics similar to ArrayList?

That would be scala.collection.mutable.ArrayBuffer.

这将是scala.collection.mutable.ArrayBuffer。

What's a good replacement for Array in Scala?

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 its co-variance a danger, it accepts primitives where generics don't, and it has a pretty limited set of methods.

好消息是,你可以在Scala中使用数组!在Java中,由于数组与泛型不兼容,所以常常避免使用数组。它是一个协变集合,而泛型是不变的,它是可变的——这使它的协变成为一种危险,它接受泛型不接受的原语,并且它有一组非常有限的方法。

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.

在Scala中,数组(在Java中仍然是相同的数组)是不变的,这使得大多数问题都消失了。Scala接受AnyVal(等价的原语)作为它的“泛型”的类型,即使它会自动装箱。通过“充实我的库”模式,所有Seq方法都可以用于数组。

So, if you want a more powerful Array, just use an Array.

所以,如果你想要一个更强大的数组,只需使用一个数组。

What about a collection that shrinks and grows?

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)

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.

然后ys将是一个新的集合,而xs仍然与此命令之前相同。无论xs是什么,这都是正确的:数组、列表等等。

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.

当然,这是要付出代价的——毕竟,你正在制作一个新的系列。Scala的不可变集合能够更好地处理这种开销,因为它们是持久性的,但这取决于执行什么操作。

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.

对于筛选器来说,任何集合都不能起太大作用,但通过在元素前挂起或删除head(实际上是堆栈的基本操作)来生成新集合,列表具有出色的性能。Vector在很多操作上都有很好的性能,但是它只有在集合不是很小的时候才有价值。例如,对于100个元素的集合,总体成本可能超过收益。

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将为您生成一个新的数组,但这样做时,您将付出完整副本的代价。

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.

Scala可变集合添加了其他一些方法。特别是,可以增加或减少大小(而不产生新集合)的集合实现了可增长和可收缩的特性。虽然它们不能保证这些操作的良好性能,但是它们会将您指向您想要检出的集合。

#2


20  

It's ArrayBuffer from scala.collection.mutable. You can find the scaladocs here.

从scala.collection.mutable ArrayBuffer。你可以在这里找到scaladocs。

#3


0  

Did you have a look at ArraySeq?

你看过ArraySeq吗?

#4


0  

It's hard to say exactly what you should do because you haven't said what behavior of ArrayList you're interested in using. It's more useful to think in terms of which scala traits you want to take advantage of. Here's a good explanation: http://grahamhackingscala.blogspot.com/2010/02/how-to-convert-java-list-to-scala-list.html.

很难确切地说出你应该做什么,因为你没有说过你对使用的ArrayList有什么行为。考虑您想要利用的scala特性更有用。这里有一个很好的解释:http://grahamhackingscala.blogspot.com/2010/02/howto -convert java-list-to-scala-list.html。

That said, you probably want some sort of IndexedSeq.

也就是说,您可能需要某种IndexedSeq。

#1


98  

I can think of 3 more specific questions to address yours:

我可以想出三个更具体的问题来回答你:

  • What is Scala's default collection?
  • Scala的默认集合是什么?
  • What Scala collection has characteristics similar to ArrayList?
  • 什么Scala集合具有与ArrayList相似的特性?
  • What's a good replacement for Array in Scala?
  • Scala中的数组有什么好的替代品?

So here are the answers for these:

这些问题的答案如下:

What is Scala's default collection?

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.

Scala等价于Java的列表接口是Seq。还有一个更通用的接口,即GenSeq——主要的区别在于,GenSeq可以根据实现的不同,以串行方式或并行方式处理操作。

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允许程序员将Seq用作工厂,所以他们通常不需要定义特定的实现,除非他们关心它。当它们这样做时,它们通常会选择Scala的列表或向量。它们都是不可变的,而且Vector具有良好的索引访问性能。另一方面,List做得很好,它做得很好。

What Scala collection has characteristics similar to ArrayList?

That would be scala.collection.mutable.ArrayBuffer.

这将是scala.collection.mutable.ArrayBuffer。

What's a good replacement for Array in Scala?

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 its co-variance a danger, it accepts primitives where generics don't, and it has a pretty limited set of methods.

好消息是,你可以在Scala中使用数组!在Java中,由于数组与泛型不兼容,所以常常避免使用数组。它是一个协变集合,而泛型是不变的,它是可变的——这使它的协变成为一种危险,它接受泛型不接受的原语,并且它有一组非常有限的方法。

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.

在Scala中,数组(在Java中仍然是相同的数组)是不变的,这使得大多数问题都消失了。Scala接受AnyVal(等价的原语)作为它的“泛型”的类型,即使它会自动装箱。通过“充实我的库”模式,所有Seq方法都可以用于数组。

So, if you want a more powerful Array, just use an Array.

所以,如果你想要一个更强大的数组,只需使用一个数组。

What about a collection that shrinks and grows?

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)

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.

然后ys将是一个新的集合,而xs仍然与此命令之前相同。无论xs是什么,这都是正确的:数组、列表等等。

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.

当然,这是要付出代价的——毕竟,你正在制作一个新的系列。Scala的不可变集合能够更好地处理这种开销,因为它们是持久性的,但这取决于执行什么操作。

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.

对于筛选器来说,任何集合都不能起太大作用,但通过在元素前挂起或删除head(实际上是堆栈的基本操作)来生成新集合,列表具有出色的性能。Vector在很多操作上都有很好的性能,但是它只有在集合不是很小的时候才有价值。例如,对于100个元素的集合,总体成本可能超过收益。

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将为您生成一个新的数组,但这样做时,您将付出完整副本的代价。

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.

Scala可变集合添加了其他一些方法。特别是,可以增加或减少大小(而不产生新集合)的集合实现了可增长和可收缩的特性。虽然它们不能保证这些操作的良好性能,但是它们会将您指向您想要检出的集合。

#2


20  

It's ArrayBuffer from scala.collection.mutable. You can find the scaladocs here.

从scala.collection.mutable ArrayBuffer。你可以在这里找到scaladocs。

#3


0  

Did you have a look at ArraySeq?

你看过ArraySeq吗?

#4


0  

It's hard to say exactly what you should do because you haven't said what behavior of ArrayList you're interested in using. It's more useful to think in terms of which scala traits you want to take advantage of. Here's a good explanation: http://grahamhackingscala.blogspot.com/2010/02/how-to-convert-java-list-to-scala-list.html.

很难确切地说出你应该做什么,因为你没有说过你对使用的ArrayList有什么行为。考虑您想要利用的scala特性更有用。这里有一个很好的解释:http://grahamhackingscala.blogspot.com/2010/02/howto -convert java-list-to-scala-list.html。

That said, you probably want some sort of IndexedSeq.

也就是说,您可能需要某种IndexedSeq。