This article will discuss different ways to create a mutable, unmodifiable, immutable, and fixed-length 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 we can modify their underlying map. Maps that 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 them. Only the reference to the map is final.

1. Mutable Empty Map

⮚ Using Plain Java

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

⮚ Using Guava

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

⮚ Java 8

We can use the Java 8 Stream 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

⮚ Using Collections

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

⮚ Using Apache Collections: MapUtils Class

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

 
Both the above methods throw 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 an immutable empty map in Java. The map will throw an UnsupportedOperationException if any modification operation is performed on it.

⮚ Using Collections

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

 
The above method might throw an unchecked assignment warning. The following example demonstrates the type-safe way to obtain an empty map:

⮚ In Java 8

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

⮚ Using Guava

Guava provides several static utility methods that can be used to obtain an 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 create an immutable empty map instance similarly.

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

⮚ Using Apache Collections

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

⮚ In Java 9

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

We can use Map.of() to create the 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 a fixedSizeMap() method that can return a fixed-length empty map backed by the specified empty map. We cannot add elements to the returned map, and the map will throw an UnsupportedOperationException if any resize operation is performed on it.

That’s all about mutable, unmodifiable, and immutable empty Map in Java.