Mutable, unmodifiable, and immutable empty Map in Java
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.
|
1 |
Map<String, String> map = new HashMap<>(); |
⮚ Using Guava
Guava’s Maps.newHashMap() creates a mutable, empty HashMap instance. This method is deprecated in Java 7 and above.
|
1 |
Map<String, String> map = Maps.newHashMap(); |
⮚ 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.
|
1 2 |
Map<String, String> map = Stream.of(new String[][]{}) .collect(Collectors.toMap(p -> p[0], p -> p[1])); |
2. Unmodifiable Empty Map
⮚ Using Collections
Collections unmodifiableMap() returns an unmodifiable view of the specified map.
|
1 2 |
Map<String, String> mutableMap = new HashMap<>(); Map<String, String> unmodifiable = Collections.unmodifiableMap(mutableMap); |
⮚ Using Apache Collections: MapUtils Class
Apache Commons Collections MapUtils.unmodifiableMap() returns an unmodifiable map backed by the specified map.
|
1 2 |
Map<String, String> mutableMap = new HashMap<>(); Map<String, String> unmodifiable = MapUtils.unmodifiableMap(mutableMap); |
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.
|
1 |
Map<String, String> immutableMap = Collections.EMPTY_MAP; |
The above method might throw an unchecked assignment warning. The following example demonstrates the type-safe way to obtain an empty map:
|
1 |
Map<String, String> immutableMap = Collections.emptyMap(); |
⮚ In Java 8
We could adapt the Collectors.toMap() collector discussed earlier to always produce an immutable empty map, as shown below:
|
1 2 3 4 5 6 7 8 |
Map<String, String> immutableMap = Stream.of(new String[][] {}) .collect(Collectors.collectingAndThen ( Collectors.toMap(p -> p[0], p -> p[1]), Collections::<String, String>unmodifiableMap ) ); |
⮚ 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.
|
1 2 |
Map<String, String> mutableMap = Maps.newHashMap(); ImmutableMap<String, String> immutableMap = ImmutableMap.copyOf(mutableMap); |
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.
|
1 |
ImmutableMap<String, String> immutableMap = new ImmutableMap.Builder().build(); |
3. ImmutableMap.of() can also be used to return an immutable empty map.
|
1 |
Map<String, String> immutableMap = ImmutableMap.of(); |
⮚ Using Apache Collections
Apache Commons Collections provides MapUtils.EMPTY_MAP that returns an immutable empty map.
|
1 |
Map<String, String> immutableMap = MapUtils.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.
|
1 |
Map<String, String> immutableMap = Map.of(); |
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.
|
1 |
Map<String, String> fixedLengthMap = MapUtils.fixedSizeMap(new HashMap<>()); |
That’s all about mutable, unmodifiable, and immutable empty Map in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)