Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting immutable to mutable collections

Tags:

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

like image 604
barroco Avatar asked Jul 05 '10 09:07

barroco


People also ask

How can we convert mutable to immutable?

If you just want a mutable HashMap , you can just use x. toMap in 2.8 or collection. immutable. Map(x.

Are collections in Java mutable?

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.

Is a mutable collection?

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.


1 Answers

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)
like image 160
barroco Avatar answered Sep 28 '22 12:09

barroco