Scala mutable和immutable类型转换

一般而言,from mutable to immutable, 使用 to 系列方法in mutable collections, like MutableList and ListBuffer’s toList method.
from immutable to mutable, 使用构造函数: scala.collection.mutable.ListBuffer(immtableList: _
).

Note that the to* methods like toList, toMap are is performed in constant time.

Map

1
2
3
4
5
6
// from mutable to immutable
val mutableMap1 = immutableMap.toMap() // after 2.8
val mutbaleMap2 = collection.immutable.Map(x.toList: _*) // before 2.8
// from immutable to mutable
val immutableMap = scala.collection.immutable.Map(1 -> "1", 2 -> "2")
val mutableMap = scala.collection.mutable.Map(immutableMap: _*)

List

1
2
3
4
5
// from mutable to immutable
val immutableList = mutableListBuffer.toList
// from immutable to mutable
val immutableList = scala.collection.immutable.List(1, 2, 3)
val mutableListBuffer = scala.collection.mutable.ListBuffer(immutableList: _*)

参考链接