Immutable Map in Java
- ImmutableMap, as suggested by the name, is an immutable type of Map. It means that the content of the map is fixed or constant after the declaration, that is, they are read-only.
- If any attempt made to add, delete and update elements in the Map, UnsupportedOperationException is thrown.
- An ImmutableMap does not allow the null element either.
- If any attempt made to create an ImmutableMap with a null element, NullPointerException is thrown. If any attempt is made to add a null element in Map, UnsupportedOperationException is thrown.
Advantages of ImmutableMap
- They are thread-safe.
- They are memory efficient.
- Since they are immutable, hence they can be passed over to third-party libraries without any problem.
Note that it is an immutable collection, not a collection of immutable objects, so the objects inside it can be modified.
Class Declaration:
@GwtCompatible(serializable=true,
emulated=true)
public abstract class ImmutableMap
extends Object
implements Map, Serializable
Class hierarchy:
java.lang.Object ? com.google.common.collect.ImmutableMap
Creating ImmutableMap
ImmutableMap can be created by various methods. These include:
- From existing Map using copy() function of Guavafilter_none
edit
play_arrow
brightness_4
// Below is the Java program to create ImmutableMapimportcom.google.common.collect.ImmutableMap;importjava.util.HashMap;importjava.util.Map;classMapUtil {// Function to create ImmutableMap from Mappublicstatic<K, T>voidiMap(Map<K, T> map){// Create ImmutableMap from Map using copyOf()ImmutableMap<K, T> immutableMap = ImmutableMap.copyOf(map);// Print the ImmutableMapSystem.out.println(immutableMap);}publicstaticvoidmain(String[] args){Map<Integer, String> map =newHashMap<Integer, String>();map.put(1,"Geeks");map.put(2,"For");map.put(3,"Geeks");iMap(map);}}Output:
{1=Geeks, 2=For, 3=Geeks} - New ImmutableMap using of() function from Guavafilter_none
edit
play_arrow
brightness_4
// Below is the Java program to create ImmutableMapimportcom.google.common.collect.ImmutableMap;importjava.util.HashMap;importjava.util.Map;classMapUtil {// Function to create ImmutableMappublicstaticvoidcreateImmutableMap(){// Create ImmutableMap using of()ImmutableMap<Integer, String> immutableMap = ImmutableMap.of(1,"Geeks",2,"For",3,"Geeks");// Print the ImmutableMapSystem.out.println(immutableMap);}publicstaticvoidmain(String[] args){createImmutableMap();}}Output:
{1=Geeks, 2=For, 3=Geeks} - Using Java 9 Factory Of() method
In Java, use of() with Set, Map or List to create an Immutable Map.
Please Note: The programs below are of Java 9. Hence you would need a Java 9 compiler to run them.
filter_noneedit
play_arrow
brightness_4
// Java code illustrating of() method to// create a ImmutableSetimportjava.util.*;importcom.google.common.collect.ImmutableMap;classGfG {publicstaticvoidmain(String args[]){// non-empty immutable setMap<Integer, String> map = Map.of(1,"Geeks",2,"For",3,"Geeks");// Let's print the setSystem.out.println(map);}}Output:
{1=Geeks, 2=For, 3=Geeks} - Using Builder() from ImmutableMap
In Guava, ImmnutableMap class provides a function Builder(). Through this function, a new ImmutableMap can be created, or
an ImmutableMap can be created from an existing Map or both.- Creating a new ImmutableMapfilter_none
edit
play_arrow
brightness_4
// Java code illustrating of() method to// create a ImmutableSetimportjava.util.*;importcom.google.common.collect.ImmutableMap;classGfG {publicstaticvoidmain(String args[]){// non-empty immutable setImmutableMap<Integer, String> imap =ImmutableMap.<Integer, String>builder().put(1,"Geeks").put(2,"For").put(3,"Geeks").build();// Let's print the setSystem.out.println(imap);}}Output:
{1=Geeks, 2=For, 3=Geeks} - Creating an ImmutableMap from existing Mapfilter_none
edit
play_arrow
brightness_4
// Java code illustrating of() method to// create a ImmutableSetimportjava.util.*;importcom.google.common.collect.ImmutableMap;classGfG {publicstaticvoidmain(String args[]){// non-empty immutable setMap<Integer, String> map = Map.of(1,"Geeks",2,"For",3,"Geeks");ImmutableMap<Integer, String> imap =ImmutableMap.<Integer, String>builder().putAll(map).build();// Let's print the setSystem.out.println(imap);}}Output:
{1=Geeks, 2=For, 3=Geeks} - Creating a new ImmutableMap including the existing Mapfilter_none
edit
play_arrow
brightness_4
// Java code illustrating of() method to// create a ImmutableSetimportjava.util.*;importcom.google.common.collect.ImmutableMap;classGfG {publicstaticvoidmain(String args[]){// non-empty immutable setMap<Integer, String> map = Map.of(1,"Geeks",2,"For",3,"Geeks");ImmutableMap<Integer, String> imap =ImmutableMap.<Integer, String>builder().putAll(map).put(4,"Computer").put(5,"Portal").build();// Let's print the setSystem.out.println(imap);}}Output:
{1=Geeks, 2=For, 3=Geeks, 4=Computer, 5=Portal}
- Creating a new ImmutableMapfilter_none
Try to change ImmutableMap
As mentioned earlier, the below program will throw UnsupportedOperationException.
filter_none
edit
play_arrow
brightness_4
|
|
Output :
Exception in thread "main" java.lang.UnsupportedOperationException
at com.google.common.collect.ImmutableCollection.add(ImmutableCollection.java:218)
at ImmutableListDemo.main(Main.java:16)
How is it different from Collections.unmodifiableMap()?
Collections.unmodifiableMap create a wrapper around the same existing Map such that the wrapper cannot be used to modify it. However, we can still change the original Map.
filter_none
edit
play_arrow
brightness_4
|
|
Output:
{1=Geeks, 2=For, 3=Geeks, 4=Computer, 5=Portal}
If we create an ImmutableMap from an existing Map and change the existing Map, the ImmutableMap does not change because a copy is created.
filter_none
edit
play_arrow
brightness_4
|
|
Output :
{1=Geeks, 2=For, 3=Geeks}




post a comment