Scala 集合中的可变与不可变
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8287425/
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
mutable vs. immutable in Scala collections
提问by astay13
I am fairly new to Scala and am trying to understand the collections hierarchy. I see that there is a distinction between 'mutable' and 'immutable' collections, but I don't understand what this actually means at the implementation level and how this relates to valand var. Can anyone give me some insight on this? Also, does every collection class have a 'mutable' version and an 'immutable' version, or are there some classes which can only be 'mutable' or 'immutable'?
我对 Scala 还很陌生,正在尝试了解集合层次结构。我看到“可变”和“不可变”集合之间存在区别,但我不明白这在实现级别实际上意味着什么以及它与val和 的关系var。谁能给我一些这方面的见解?此外,是否每个集合类都有一个“可变”版本和一个“不可变”版本,或者是否有一些类只能是“可变”或“不可变”?
回答by Fred Foo
Mutable means you can alter the collection in-place. So, if you have a collection cand you append an element with +=, then chas changed, and so has every other reference to that collection.
可变意味着您可以就地更改集合。因此,如果您有一个集合c并且您附加了一个带有 的元素+=,则已c更改,对该集合的所有其他引用也是如此。
Immutable means that the collection object never changes; instead, you build new collection objects with operations such as +or ++, which return a new collection. This is useful in concurrent algorithms, since it requires no locking to add something to a collection. It may come at the cost of some overhead, but this property can be very useful. Scala's immutable collections are fully persistent data structures.
不可变意味着集合对象永远不会改变;相反,您可以使用+或等操作构建新的集合对象,这些操作++会返回一个新的集合。这在并发算法中很有用,因为它不需要锁定来向集合添加内容。它可能会以一些开销为代价,但此属性可能非常有用。Scala 的不可变集合是完全持久的数据结构。
The difference is very similar to that between varand val, but mind you:
所不同的是非常相似之间var和val,但请不要忘记:
- You can modify a mutable collection bound to a
valin-place, though you can't reassign theval - you can't modify an immutable collection in-place, but if it's assigned to a
var, you can reassign thatvarto a collection built from it by an operation such as+.
- 您可以修改绑定到
val就地的可变集合,但您不能重新分配val - 您无法就地修改不可变集合,但如果将其分配给 a
var,则可以将其重新分配var给通过诸如+.
Not all collections necessarily exist in mutable and immutable variants; the last time I checked, only mutable priority queues were supported.
并非所有集合都必须存在于可变和不可变变体中;我上次检查时,只支持可变优先级队列。
回答by agilesteel
Immutable means unchangeable. valmakes a reference unchangeable, which means you cannot assign a value to a valonce it has been initialized. Immutable collections make the collection itself unchangeable not the reference to it. Each time you modify an immutable collection, another collection is produced instead of modifying the original collection in-place. Most collections have both immutable and mutable versions, but there are exceptions of course.
不可变意味着不可改变。val使引用不可更改,这意味着val一旦初始化就不能为 a 赋值。不可变集合使集合本身不可更改,而不是对它的引用。每次修改不可变集合时,都会生成另一个集合,而不是就地修改原始集合。大多数集合都有不可变和可变版本,但当然也有例外。

