如何在 Scala 中将 immutable.Map 转换为 mutable.Map?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5042878/
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 can I convert immutable.Map to mutable.Map in Scala?
提问by ?ukasz Lew
How can I convert immutable.Mapto mutable.Mapin Scala so I can update the values in Map?
如何在 Scala 中转换immutable.Map为mutable.Map以便更新 中的值Map?
回答by Kevin Wright
The cleanest way would be to use the mutable.Mapvarargs factory. Unlike the ++approach, this uses the CanBuildFrommechanism, and so has the potential to be more efficient if library code was written to take advantage of this:
最干净的方法是使用mutable.Mapvarargs 工厂。与该++方法不同,它使用该CanBuildFrom机制,因此如果编写库代码以利用此机制,则有可能更高效:
val m = collection.immutable.Map(1->"one",2->"Two")
val n = collection.mutable.Map(m.toSeq: _*)
This works because a Mapcan also be viewed as a sequence of Pairs.
这是有效的,因为 aMap也可以被视为一系列 Pairs。
回答by Rex Kerr
val myImmutableMap = collection.immutable.Map(1->"one",2->"two")
val myMutableMap = collection.mutable.Map() ++ myImmutableMap
回答by ymnk
How about using collection.breakOut?
使用 collection.breakOut 怎么样?
import collection.{mutable, immutable, breakOut}
val myImmutableMap = immutable.Map(1->"one",2->"two")
val myMutableMap: mutable.Map[Int, String] = myImmutableMap.map(identity)(breakOut)
回答by Xavier Guihot
Starting Scala 2.13, via factory builders applied with .to(factory):
开始Scala 2.13,通过工厂建设者应用.to(factory):
Map(1 -> "a", 2 -> "b").to(collection.mutable.Map)
// collection.mutable.Map[Int,String] = HashMap(1 -> "a", 2 -> "b")
回答by Alexander Azarov
There is a variant to create an empty mutable Mapthat has default values taken from the immutable Map. You may store a value and override the default at any time:
有一个变体可以创建一个空的 mutable Map,它的默认值取自 immutable Map。您可以随时存储一个值并覆盖默认值:
scala> import collection.immutable.{Map => IMap}
//import collection.immutable.{Map=>IMap}
scala> import collection.mutable.HashMap
//import collection.mutable.HashMap
scala> val iMap = IMap(1 -> "one", 2 -> "two")
//iMap: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,one), (2,two))
scala> val mMap = new HashMap[Int,String] {
| override def default(key: Int): String = iMap(key)
| }
//mMap: scala.collection.mutable.HashMap[Int,String] = Map()
scala> mMap(1)
//res0: String = one
scala> mMap(2)
//res1: String = two
scala> mMap(3)
//java.util.NoSuchElementException: key not found: 3
// at scala.collection.MapLike$class.default(MapLike.scala:223)
// at scala.collection.immutable.Map$Map2.default(Map.scala:110)
// at scala.collection.MapLike$class.apply(MapLike.scala:134)
// at scala.collection.immutable.Map$Map2.apply(Map.scala:110)
// at $anon.default(<console>:9)
// at $anon.default(<console>:8)
// at scala.collection.MapLike$class.apply(MapLike.scala:134)....
scala> mMap(2) = "three"
scala> mMap(2)
//res4: String = three
Caveat(see the comment by Rex Kerr): You will not be able to remove the elements coming from the immutable map:
警告(请参阅 Rex Kerr 的评论):您将无法删除来自不可变映射的元素:
scala> mMap.remove(1)
//res5: Option[String] = None
scala> mMap(1)
//res6: String = one

