In the previous tutorials we learned about the factory methods introduced in Java 9 to create immutable List and immutable Set. In this guide, we will learn how to create immutable Map and Map.Entry by using Java 9 Factory methods.
1. Creating Immutable Map prior to Java 9
Before we see how to create immutable Map using Java 9 factory methods, lets see how we used to create them before Java 9.
1.1 Creating Empty Map before Java 9
We used to use the unmodifiableMap() method of Collections class to create unmodifiable(immutable) Map.
Map<String,String> map = new HashMap<String, String>(); Map<String,String> immutableMap = Collections.unmodifiableMap(map);
Lets test this in JShell.
1.2 Creating Non-Empty Map before Java 9
Map<String,String> map = new HashMap<String, String>(); map.put("Key1", "Jon"); map.put("Key2", "Steve"); map.put("Key3", "Mia"); map.put("Key4", "Lora"); Map<String,String> immutableMap = Collections.unmodifiableMap(map);
Lets test this in JShell.
2. Java 9 Factory Methods to create immutable Map
There are couple of useful factory methods introduced in Java 9 to create unmodifiable Map. We will take the same examples that we have seen above to compare how things got easier in Java 9. The lines of code that we had to write prior to Java 9 is reduced drastically with the help of factory methods.
2.1 Java 9 – Immutable Empty Map using Map.of() factory method
Method that we use for empty Map:
static <K,V> Map<K,V> of()
Example:
Map<String,String> immutableMap = Map.of()
Testing in JShell:
jshell> Map<String,String> immutableMap = Map.of() immutableMap ==> {}
2.2 Java 9 – Immutable Non-Empty Map using Map.of(Key, Value….) factory method
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2...)
Example:
To demonstrate the use of Map.of() factory method, we have taken the same example that we have seen above using unmodifiableMap() method. As you can see how simple it is in Java 9. We have reduced the 6 lines of code in a single line.
Map<String, String> immutableMap = Map.of("Key1", "Jon", "Key2", "Steve", "Key3", "Mia", "Key4", "Lora")
Testing in JShell:
jshell> Map<String, String> immutableMap = Map.of("Key1", "Jon", "Key2", "Steve", "Key3", "Mia", "Key4", "Lora") immutableMap ==> {Key3=Mia, Key4=Lora, Key1=Jon, Key2=Steve}
What is an immutable Map?
1. Immutable Map doesn’t allow addition, deletion and update of its elements. If you try to perform these operations then the program will throw UnsupportedOperationException
.
jshell> Map<String, String> immutableMap = Map.of("Key1", "Jon", "Key2", "Steve", "Key3", "Mia", "Key4", "Lora") immutableMap ==> {Key3=Mia, Key4=Lora, Key1=Jon, Key2=Steve} jshell> immutableMap.put("Key5", "Chaitanya"); | java.lang.UnsupportedOperationException thrown: | at ImmutableCollections.uoe (ImmutableCollections.java:71) | at ImmutableCollections$AbstractImmutableMap.put (ImmutableCollections.java:558) | at (#2:1)
2. They do not allow null elements. Adding null elements will throw the same UnsupportedOperationException
.
Leave a Reply