如何在 Scala 中使用可变集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6909783/
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
How to use mutable collections in Scala
提问by Henry Henrinson
I think I may be failing to understand how mutable collections work. I would expect mutable collections to be affected by applying map to them or adding new elements, however:
我想我可能无法理解可变集合的工作原理。我希望可变集合会受到对它们应用 map 或添加新元素的影响,但是:
scala> val s: collection.mutable.Seq[Int] = collection.mutable.Seq(1)
s: scala.collection.mutable.Seq[Int] = ArrayBuffer(1)
scala> s :+ 2 //appended an element
res32: scala.collection.mutable.Seq[Int] = ArrayBuffer(1, 2)
scala> s //the original collection is unchanged
res33: scala.collection.mutable.Seq[Int] = ArrayBuffer(1)
scala> s.map(_.toString) //mapped a function to it
res34: scala.collection.mutable.Seq[java.lang.String] = ArrayBuffer(1)
scala> s //original is unchanged
res35: scala.collection.mutable.Seq[Int] = ArrayBuffer(1)
//maybe mapping a function that changes the type of the collection shouldn't work
//try Int => Int
scala> s.map(_ + 1)
res36: scala.collection.mutable.Seq[Int] = ArrayBuffer(2)
scala> s //original unchanged
res37: scala.collection.mutable.Seq[Int] = ArrayBuffer(1)
This behaviour doesn't seem to be separate from the immutable collections, so when do they behave separately?
这种行为似乎与不可变集合并没有分开,那么它们什么时候分开表现呢?
回答by Jean-Philippe Pellet
For both immutable and mutable collections, :+and +:create new collections. If you want mutable collections that automatically grow, use the +=and +=:methods defined by collection.mutable.Buffer.
对于不可变和可变集合,:+并+:创建新集合。如果您想要自动增长的可变集合,请使用+=和+=:定义的方法collection.mutable.Buffer。
Similarly, mapreturns a new collection — look for transformto change the collection in place.
类似地,map返回一个新的集合——寻找transform改变集合的地方。
回答by missingfaktor
mapoperation applies the given function to all the elements of collection, and produces a new collection.
map操作将给定的函数应用于集合的所有元素,并产生一个新的集合。
The operation you are looking for is called transform. You can think of it as an in-place mapexcept that the transformation function has to be of type a -> ainstead of a -> b.
您正在寻找的操作称为transform。你可以把它作为一个就地map除了变换函数必须是类型的a -> a,而不是a -> b。
scala> import collection.mutable.Buffer
import collection.mutable.Buffer
scala> Buffer(6, 3, 90)
res1: scala.collection.mutable.Buffer[Int] = ArrayBuffer(6, 3, 90)
scala> res1 transform { 2 * }
res2: res1.type = ArrayBuffer(12, 6, 180)
scala> res1
res3: scala.collection.mutable.Buffer[Int] = ArrayBuffer(12, 6, 180)
回答by Ben James
The mapmethod never modifies the collection on which you call it. The type system wouldn't allow such an in-place map implementation to exist - unless you changed its type signature, so that on some type Collection[A]you could only map using a function of type A => A.
该map方法永远不会修改您调用它的集合。类型系统不允许存在这样的就地映射实现——除非你改变了它的类型签名,这样在某些类型上Collection[A]你只能使用 type 的函数进行映射A => A。
(Edit: as other answers have pointed out, there is such a method called transform!)
(编辑:正如其他答案所指出的,有一种方法叫做transform!)
Because mapcreates a new collection, you can go from a Collection[A]to a Collection[B]using a function A => B, which is much more useful.
因为map创建了一个新集合,您可以使用函数从 aCollection[A]转到 a ,这更有用。Collection[B]A => B

