Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

O(1) conversion from mutable.Map to immutable.Map?

Is there a way to convert (wrap) a mutable Map to immutable in O(1) time (that is, not by copying the values, but similar to what is done in JavaConversions)

like image 958
IttayD Avatar asked Mar 17 '10 08:03

IttayD


People also ask

How do I convert a mutable Map to immutable Map in Scala?

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

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.

Is Map immutable in Javascript?

js provides many Persistent Immutable data structures including: List , Stack , Map , OrderedMap , Set , OrderedSet and Record .

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.


2 Answers

As Thomas points out, the read only view is O(1). But read-only doesn't equate to immutability.

The difference is well descrived in the "Fighting Bit Rot" paper:

All collection classes are kept in a package scala.collection. This package has three subpackages: mutable, immutable, and generic. Most collections exist in three forms, depending on their mutability.

A collection in package scala.collection.immutable is guaranteed to be immutable for everyone. That means one can rely on the fact that accessing the same collection value over time will always yield a collection with the same elements. A collection in package scala.collection.mutable is known to have some operations that change the collection in place.

A collection in package scala.collection can be either mutable or immutable. For instance, collection.Seq[T] is a superclass of both collection.immutable.Seq[T] and collection.mutable.Seq[T]. Generally, the root collections in package scala. collection define the same interface as the immutable collections, and the mutable collections in package scala.collection.mutable typically add some destructive modification operations to this immutable interface. The difference between root collections and immutable collections is that a user of an immutable collection has a guarantee that nobody can mutate the collection, whereas users of root collections have to assume modifications by others, even though they cannot do any modifications themselves.

Perhaps it's just a simple as up-casting.

scala> val mm = collection.mutable.Map(1 -> 2)
mm: scala.collection.mutable.Map[Int,Int] = Map(1 -> 2)

scala> val readOnly = mm : collection.Map[Int, Int]
readOnly: scala.collection.Map[Int,Int] = Map(1 -> 2)
like image 78
retronym Avatar answered Sep 20 '22 08:09

retronym


There is a read only projection for mutable maps.

scala> collection.mutable.Map(1->2).readOnly
res0: scala.collection.Map[Int,Int] = ro-Map(1 -> 2)

As oxbow_lakes pointed out the underlying Map is still mutable and may change after the read-only projection is published to clients. The illusion of immutability has to addressed in code managing the map.

like image 29
Thomas Jung Avatar answered Sep 19 '22 08:09

Thomas Jung