What is the best way to convert collection.immutable.Set
to collection.mutable.Set
?
If you just want a mutable HashMap , you can just use x. toMap in 2.8 or collection. immutable. Map(x.
The collections returned by the convenience factory methods added in JDK 9 are conventionally immutable. Any attempt to add, set, or remove elements from these collections causes an UnsupportedOperationException to be thrown.
A mutable collection can be updated or extended in place. This means you can change, add, or remove elements of a collection as a side effect. Immutable collections, by contrast, never change.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With