Generally, from mutable to immutable, you use the to*
series methods in mutable collections, like MutableList and ListBuffer's toList
method.
In the other hand, from immutable to mutable, you can just use constructors like this: scala.collection.mutable.ListBuffer(immtableList: _*)
.
Note that the
to*
methods like toList, toMap are is performed in constant time.
Here is some examples.
Map
// 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
// 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: _*)
Ref
[1] - How can I convert immutable.Map to mutable.Map in Scala?
[2] - Converting mutable to immutable map
[3] - Converting mutable collection to immutable