元素如何存储在集合中? [英] How elements are stored in a collection?

查看:38
本文介绍了元素如何存储在集合中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉 Java 集合的实现,我正在尝试了解集合是如何在 Scala 中实现的.

I am familiar with Java collections are implemented and I am trying to understand how collections are implemented in Scala.

Scala 的集合最终会包装 Java 集合吗?Scala 中 ArrayList 的等价物是什么?Java 中的 ArrayList 包含一个变量 elementData,其中包含一个数组 [*].这个实现与 Scala 中的等效类/特征相似吗?Scala 中的可增长集合是如何实现的?

Do Scala's collections ultimately wrap Java collections? What is the equivalent of ArrayList in Scala? ArrayList in Java contains a variable elementData that holds an array [*]. Is this implementation similar in the equivalent class/trait in Scala? How growable collections in Scala are implemented?

亲切的问候,亚历山大

[*]http:///grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/ArrayList.java

推荐答案

有一个 Scala 集合的概览文档.

基本上,这些是直接用 Scala 编写的结构,而不是对现有 Java 集合的包装,尽管可以使用 JavaConverters 对象在 Scala API 中包装 Java 集合.

Basically these are structures written directly in Scala and not wrappers around existing Java collections, although it is possible to wrap Java collections in the Scala API using the JavaConverters object.

Scala 提倡不可变的数据结构,当涉及并发时更容易推理,而标准的 Java 集合是可变的.java.util.ArrayList 的对应关系可能是 scala.collection.mutable.Buffer.默认的缓冲区实现是 ArrayBuffer,它也在内部使用一个可以动态增长的数组.

Scala advocates immutable data structures which are easier to reason about when concurrency is involved, whereas the standard Java collections are mutable. The correspondence for java.util.ArrayList would probably be scala.collection.mutable.Buffer. The default buffer implementation is ArrayBuffer which also uses an array internally which can grow dynamically.

这篇关于元素如何存储在集合中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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