Mutable, Unmodifiable and Immutable Empty Map in Java

In this article, we will discuss different ways to create mutable, unmodifiable and immutable empty map 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 their underlying map can be modified. Maps that additionally guarantee that no change in the map will ever be visible (even if their underlying map is modified) are referred to as Immutable.

Please note that making a Map final will not make it unmodifiable or immutable. We can still add key-value pairs or remove key-value pairs from it. Only the reference to the Map is final.


1. Mutable Empty Map


1.1. Using Plain Java –

We can simply use of the HashMap constructor which constructs an empty resizable-array implementation of the Map interface. Below code takes advantage of the new “diamond” syntax introduced in Java SE 7.


1.2. Using Guava –

Guava’s Maps.newHashMap() creates a creates a mutable, empty HashMap instance. This method is deprecated in Java 7 and above.


1.3. Java 8 –

The Java 8 Stream API can be used to construct empty collections, by combining stream factory methods and collectors. Collectors.toMap() returns a Collector that accumulates the input key-value pairs into a new Map. To create an empty map, we can pass an empty 2D array.


2. Unmodifiable Empty Map


2.1. Using Collections –

Collections unmodifiableMap() returns an unmodifiable view of the specified map.


2.2. Using Apache Collections: MapUtils Class –

Apache Commons Collections MapUtils.unmodifiableMap() returns an unmodifiable map backed by the specified map.

Both above methods throws a NullPointerException if the specified map is null. If we try to modify the returned map, the map will throw an UnsupportedOperationException. However, any changes in the original mutable map will be visible in the unmodifiable map.


3. Immutable Empty Map

There are several ways to create immutable empty Map in Java. The Map will throw an UnsupportedOperationException if any modify operation is performed on it.


3.1. Using Collections –

We can use Collections.EMPTY_MAP that returns serializable and immutable empty map.

Above method might throw unchecked assignment warning. Below example illustrates the type-safe way to obtain an empty map:


3.2. In Java 8 –

We could adapt the Collectors.toMap() collector discussed earlier to always produce an immutable empty map as shown below:


3.3. Using Guava –

Guava provides several static utility methods that can be used to obtain immutable empty map.

1. ImmutableMap.copyOf returns an immutable empty map if specified map is empty.

The method will throw a NullPointerException if the specified map is null and any changes in the underlying mutable map will not be visible in the immutable map.

2. Guava also provides a builder that can be used to create immutable empty map instance in similar way.

3. ImmutableMap.of() can also be used to return an immutable empty map.


3.4. Using Apache Collections –

Apache Commons Collections provides MapUtils.EMPTY_MAP that returns immutable empty map.


3.5. In Java 9 –

Java 9 comes with static factory methods on the Map interface that can creates compact, unmodifiable instances of it.

We can use Map.of() to create structurally immutable empty map. Keys and values cannot be added to it and calling any mutator method will always cause UnsupportedOperationException to be thrown.


4. Fixed Length Empty Map

There is another type of empty map possible in Java apart from mutable, unmodifiable and immutable maps called fixed-length map.

Apache Commons Collections MapUtils class has fixedSizeMap() method that can return a fixed-length empty map backed by the specified empty map. Elements cannot be added to the returned map and the map will throw an UnsupportedOperationException if any resize operation is performed on it.

Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

Notify of