UnmodifiableMap (Java collections) vs ImmutableMap (Google)

Context

I need to return the link to the card that I use for the data cache, and I would like to make sure that no one can change their link.

Question

I have seen many references to UnmodifiableMap and ImmutableMap on the Internet, but I do not see anything comparing / contrasting them. I believe there is a good reason Google / Guava created their own version - can someone tell me what it is?

+88
java collections guava map
Mar 25 '14 at 13:49
source share
4 answers

The modifiable map may still change. This is just a look at the map being modified, and changes in the support map will be visible through the unmodified map. An unmodifiable map only prevents modifications for those who have only a link to an unmodifiable representation:

Map<String, String> realMap = new HashMap<String, String>(); realMap.put("A", "B"); Map<String, String> unmodifiableMap = Collections.unmodifiableMap(realMap); // This is not possible: It would throw an // UnsupportedOperationException //unmodifiableMap.put("C", "D"); // This is still possible: realMap.put("E", "F"); // The change in the "realMap" is now also visible // in the "unmodifiableMap". So the unmodifiableMap // has changed after it has been created. unmodifiableMap.get("E"); // Will return "F". 

In contrast, the ImmutableMap from Guava is really unchanged: it is a real copy of this map, and no one can modify this ImmutableMap in any way.

Update :

As pointed out in comment , an immutable map can also be created with a standard API using

 Map<String, String> immutableMap = Collections.unmodifiableMap(new LinkedHashMap<String, String>(realMap)); 

This will create an unmodifiable view of the true copy of this map and, thus, beautifully emulates the characteristics of ImmutableMap , without adding dependencies to Guava.

+170
Mar 25 '14 at 13:55
source share

Take a look at ImmutableMap JavaDoc: doc

There is information that there:

Unlike Collections.unmodifiableMap (java.util.Map), which is a kind of separate map that can still change, the ImmutableMap instance contains its own data and will never change. ImmutableMap is convenient for public static final cards ("permanent cards"), and also makes it easy to make a "protective copy" of the card provided to your class by the caller.

+8
Mar 25 '14 at 13:52
source share

Guava Documentation

The JDK provides Collections.unmodifiableXXX methods, but in our opinion, they can be cumbersome and verbose; it’s unpleasant to use it wherever you want to make protective copies unsafe: returned collections are really immutable if no one references the original collection inefficiently: data structures still have all the overhead of mutable collections, including parallel modification checks, extra space in the hash tables etc.

+2
Mar 25 '14 at 13:51
source share

ImmutableMap is not null , whereas Collections.unmodifiableMap() does. In addition, it will not change after construction, but UnmodifiableMap can. From JavaDoc:

A persistent, hash-oriented map with a robust custom iteration order. Does not allow null keys or values.

Unlike Collections.unmodifiableMap (java.util.Map), which is a kind of separate map that can still change, the ImmutableMap instance contains its own data and will never change. ImmutableMap is convenient for public static final cards ("permanent cards"), and also makes it easy to make a "protective copy" of the card provided to your class by the caller.

+2
Mar 25 '14 at 13:53
source share



All Articles