Scala 等价于 java.util.ArrayList [英] Scala equivalent of java.util.ArrayList

查看:35
本文介绍了Scala 等价于 java.util.ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Scala 中做一个项目,但我对这门语言相当陌生并且有 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.

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

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

推荐答案

我可以想到 3 个更具体的问题来解决您的问题:

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

  • Scala 的默认集合是什么?
  • 哪些 Scala 集合具有类似于 ArrayList 的特征?
  • Scala 中 Array 的替代品是什么?
  • 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 的 List 接口是 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.

因为 Scala 允许程序员将 Seq 用作工厂,所以他们通常不会费心定义特定的实现,除非他们关心它.当他们这样做时,他们通常会选择 Scala 的 ListVector.它们都是不可变的,并且 Vector 具有良好的索引访问性能.另一方面,List 执行它擅长的操作.

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 中使用 Array!在 Java 中,Array 通常被避免使用,因为它通常与泛型不兼容.它是一个协变集合,而泛型是不变的,它是可变的——这使得它的协变成为一种危险,它接受泛型不接受的原语,并且它的方法集非常有限.

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 中,Array —— 仍然与 Java 中的 Array 相同 —— 是不变的,这使得大多数问题都消失了.Scala 接受 AnyVal(相当于原语)作为其泛型"的类型,即使它会进行自动装箱.并且通过丰富我的库"模式,Seq 方法的ALL 可用于Array.

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.

因此,如果您想要更强大的Array,只需使用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 将是一个 new 集合,而 xs 仍将与此命令之前的相同.无论 xs 是什么,这都是正确的:ArrayList 等等.

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.

没有集合可以对 filter 做很多事情,但是 List 在通过添加元素或删除头部来生成新集合时具有出色的性能——基本操作事实上,一个堆栈.Vector 在一堆操作上有很好的表现,但只有在集合不小的情况下才值得.例如,对于多达一百个元素的集合,总成本可能会超过收益.

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.

因此您实际上可以向 Array 添加或删除元素,Scala 将为您生成一个 new Array,但是您将这样做时需要支付完整副本的费用.

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 可变集合添加了一些其他方法.特别是,可以增加或减少大小的集合——而不产生新的集合——实现 GrowableShrinkable 特征.不过,它们并不能保证这些操作的良好性能,但它们会为您指明要查看的集合.

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 等价于 java.util.ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆