Unmodifiable Map in Java
This post will discuss various methods to create an unmodifiable map in Java.
Unmodifiable maps are “read-only” wrappers over other collections. They do not support any modification operations such as add, remove, and clear, but their underlying collection can be changed. Maps that additionally guarantee that no change in the Collection object will ever be visible are referred to as immutable.
Please note that making a map final will not make it Unmodifiable. We can still add elements or remove elements from it. Only the reference to the map is final.
1. Using Collections.unmodifiableMap() method
Collections unmodifiableMap() method returns an unmodifiable “read-only” view of the specified map. It takes another map and wraps it in a class that rejects modification requests, thus creating an unmodifiable view of the original map. Any attempt to modify the returned map directly or via its iterator will result in an UnsupportedOperationException.
However, it won’t create an inherently unmodifiable map. Having possession of a reference to the underlying collection still allows modification, i.e., any changes made to the original map will be reflected in the unmodifiable map. Also, each wrapper is an additional object, requiring another level of indirection and consuming more memory than the original collection. Finally, the wrapped collection still bears the expense of supporting mutation even if it is never intended to be modified.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.Collections; import java.util.HashMap; import java.util.Map; class Main { // Unmodifiable map in Java public static void main(String[] args) { Map<String, String> mutableMap = new HashMap<>(); mutableMap.put("United States", "Washington D.C."); // Using Java Collections Map<String, String> unmodifiableMap = Collections.unmodifiableMap(mutableMap); try { // any attempt to modify the map will result in // an `UnsupportedOperationException` unmodifiableMap.put("United Kingdom", "London"); } catch (UnsupportedOperationException ex) { System.out.println("java.lang.UnsupportedOperationException"); } // any changes made to the original map will be reflected // in the unmodifiable map mutableMap.remove("United States"); mutableMap.put("India", "New Delhi"); System.out.println(unmodifiableMap); } } |
Output:
java.lang.UnsupportedOperationException
{India=New Delhi}
2. Using Apache Commons Collections
Apache Commons Collections MapUtils class provides an unmodifiableMap() method that returns an unmodifiable map backed by the given map. If the given map is null, it throws a NullPointerException. The map will throw an UnsupportedOperationException if any modification operation is performed on it. However, any changes made to the original map will be reflected in the returned map.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import org.apache.commons.collections4.MapUtils; import java.util.HashMap; import java.util.Map; class Main { // Unmodifiable map in Java public static void main(String[] args) { Map<String, String> mutableMap = new HashMap<>(); mutableMap.put("United States", "Washington D.C."); // Apache Commons Collections – creates a view Map<String, String> unmodifiableMap = MapUtils.unmodifiableMap(mutableMap); try { // any attempt to modify the map will result in // an `UnsupportedOperationException` unmodifiableMap.put("United Kingdom", "London"); } catch (UnsupportedOperationException ex) { System.out.println("java.lang.UnsupportedOperationException"); } // modify the original map – changes will be reflected // back in the `unmodifiableMap` mutableMap.remove("United States"); mutableMap.put("India", "New Delhi"); System.out.println(unmodifiableMap); } } |
Output:
java.lang.UnsupportedOperationException
{India=New Delhi}
That’s all about unmodifiable Maps in Java.
Suggested Read:
Reference: JEP 269: Convenience Factory Methods for Collections
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 :)