Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting mutable to immutable map

private[this]object MMMap extends  HashMap[A, Set[B]] with MultiMap[A, B]

How convert it to immutable?

like image 755
Jeriho Avatar asked May 12 '10 08:05

Jeriho


People also ask

How can we convert mutable map to immutable in Java?

You can use myMap. toMap to convert an a mutable map into immutable in Scala 2.8 and later versions.

How do you make a mutable list immutable?

Java offers List. copyOf to convert a mutable list to an immutable one (copying it if needed), but offers no method that inverts this logic (creating a mutable List only if the input isn't mutable already).

Is map mutable or immutable?

There are two kinds of Maps, the immutable and the mutable. The difference between mutable and immutable objects is that when an object is immutable, the object itself can't be changed. By default, Scala uses the immutable Map.

Are maps mutable in Java?

Mutable maps supports modification operations such as add, remove, and clear on it. Unmodifiable Maps are “read-only” wrappers over other maps. They do not support add, remove, and clear operations, but we can modify their underlying map.


4 Answers

The immutable hierarchy doesn't contain a MultiMap, so you won't be able to use the converted structure with the same convenient syntax. But if you're happy to deal with key/valueset pairs, then:

If you just want a mutable HashMap, you can just use x.toMap in 2.8 or collection.immutable.Map(x.toList: _*) in 2.7.

But if you want the whole structure to be immutable--including the underlying set!--then you have to do more: you need to convert the sets along the way. In 2.8:

x.map(kv => (kv._1,kv._2.toSet)).toMap

In 2.7:

collection.immutable.Map(
  x.map(kv => (kv._1,collection.immutable.Set(kv._2.toList: _*))).toList: _*
)
like image 144
Rex Kerr Avatar answered Oct 05 '22 12:10

Rex Kerr


scala> val mutableMap = new HashMap[Int, String]
mutableMap: scala.collection.mutable.HashMap[Int,String] = Map()

scala> mutableMap += 1 -> "a"
res5: mutableMap.type = Map((1,a))

scala> mutableMap
res6: scala.collection.mutable.HashMap[Int,String] = Map((1,a))

scala> val immutableMap = mutableMap.toMap
immutableMap: scala.collection.immutable.Map[Int,String] = Map((1,a))

scala> immutableMap += 2 -> "b"
<console>:11: error: reassignment to val
       immutableMap += 2 -> "b"
                ^
like image 25
michael.kebe Avatar answered Oct 05 '22 14:10

michael.kebe


You can use myMap.toMap to convert an a mutable map into immutable in Scala 2.8 and later versions.

Looking at definition of toMap from documentation:

def toMap[T, U](implicit ev: A <:< (T, U)): immutable.Map[T, U] = {
  val b = immutable.Map.newBuilder[T, U]
  for (x <- self) b += x
  b.result
}
like image 43
Fahad Siddiqui Avatar answered Oct 05 '22 13:10

Fahad Siddiqui


You can just to the following

val imm_map = MMMap.toMap
like image 24
Harish Gajapathy Avatar answered Oct 05 '22 14:10

Harish Gajapathy