VOOZH about

URL: https://www.geeksforgeeks.org/java/immutable-map-in-java/

⇱ Immutable Map in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Immutable Map in Java

Last Updated : 11 Jul, 2025
  • ImmutableMap, as suggested by the name, is a type of Map which is immutable. It means that the content of the map are fixed or constant after 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 null element either.
  • If any attempt made to create an ImmutableMap with null element, NullPointerException is thrown. If any attempt is made to add 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 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:

  1. From existing Map using copyOf() function of Guava 
  1. Output:
{1=Geeks, 2=For, 3=Geeks}
  1. New ImmutableMap using of() function from Guava 
  1. Output:
{1=Geeks, 2=For, 3=Geeks}
  1. 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. 
  1. Output:
{1=Geeks, 2=For, 3=Geeks}
  1. Using Builder() from ImmutableMap In Guava, ImmutableMap 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 ImmutableMap 
  • Output:
{1=Geeks, 2=For, 3=Geeks}
  • Creating an ImmutableMap from existing Map 
  • Output:
{1=Geeks, 2=For, 3=Geeks}
  • Creating a new ImmutableMap including the existing Map 
  • Output:
{1=Geeks, 2=For, 3=Geeks, 4=Computer, 5=Portal}


Try to change ImmutableMap As mentioned earlier, the below program will throw UnsupportedOperationException

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 creates a wrapper around the same existing Map such that the wrapper cannot be used to modify it. However we can still change original Map. 

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. 

Output :

{1=Geeks, 2=For, 3=Geeks}

Another approach:

1) Using Collections.singletonMap()

Description: The Collections.singletonMap() method returns an immutable map containing only one mapping. It is used to create a map with a single key-value pair.

Syntax: 

Collections.singletonMap(key, value) 

Example:


Output
{gfg=25}
Note: This will throw an UnsupportedOperationException because the map is immutable
Comment