Converting immutable to mutable collections

barroco picture barroco · Jul 5, 2010 · Viewed 9.7k times · Source

What is the best way to convert collection.immutable.Set to collection.mutable.Set?

Answer

barroco picture barroco · Jul 5, 2010
scala> var a=collection.mutable.Set[Int](1,2,3)                              
a: scala.collection.mutable.Set[Int] = Set(1, 2, 3)

scala> var b=collection.immutable.Set[Int](1,2,3)
b: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

scala> collection.mutable.Set(b.toArray:_*)      
res0: scala.collection.mutable.Set[Int] = Set(1, 2, 3)

scala> collection.mutable.Set(b.toSeq:_*)  
res1: scala.collection.mutable.Set[Int] = Set(1, 2, 3)

scala> collection.mutable.Set(b.toList:_*)
res2: scala.collection.mutable.Set[Int] = Set(1, 2, 3)