A possible downside of Map.of is that it returns Map, which provides a lot of methods that are documented to throw UnsupportedOperationException in case the map is immutable - that may be considered a disadvantage in API design. There is no reliable way for code consuming Maps to know whether any of these will work.

There were good reasons for this decision, such as being able to take advantage of the existing codebase without breaking backwards compatibility. But this comes at the cost that the type system can't help with communicating whether a given method can accept (or requires) an immutable or a mutable map. That is left to documentation.

(guavas ImmutableMap also implements Map, but at least the unsupported methods are marked as deprecated. Arguably still not ideal, but... tradeoffs)

Answer from Hulk on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java collections › java map › immutable map implementations in java
Immutable Map Implementations in Java | Baeldung
January 23, 2024 - An Immutable Map, on the other hand, contains its own private data and doesn’t allow modifications to it. Therefore, the data cannot change in any way once an instance of the Immutable Map is created. Guava provides immutable versions of each java.util.Map using ImmutableMap.
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java immutable vs. unmodifiable map: what’s difference?
Java Immutable Vs. Unmodifiable Map: What's Difference?
July 1, 2024 - The Map.of() method was introduced in Java 9. Using this method, we can create an immutable map containing zero or up to 10 key-value pairs.
🌐
Javacodehouse
javacodehouse.com › blog › java-map-of
Creating Immutable Maps with Java
January 24, 2024 - In Java, an immutable map is a map that cannot be modified after it has been created. Once an immutable map is created, its contents cannot be changed, which offers several benefits in terms of thread-safety.
🌐
GeeksforGeeks
geeksforgeeks.org › java › immutable-map-in-java
Immutable Map in Java - GeeksforGeeks
July 11, 2025 - Creating ImmutableMap ImmutableMap can be created by various methods. These include: From existing Map using copyOf() function of Guava ... // Below is the Java program to create ImmutableMap import com.google.common.collect.ImmutableMap; import ...
Top answer
1 of 3
12

A possible downside of Map.of is that it returns Map, which provides a lot of methods that are documented to throw UnsupportedOperationException in case the map is immutable - that may be considered a disadvantage in API design. There is no reliable way for code consuming Maps to know whether any of these will work.

There were good reasons for this decision, such as being able to take advantage of the existing codebase without breaking backwards compatibility. But this comes at the cost that the type system can't help with communicating whether a given method can accept (or requires) an immutable or a mutable map. That is left to documentation.

(guavas ImmutableMap also implements Map, but at least the unsupported methods are marked as deprecated. Arguably still not ideal, but... tradeoffs)

2 of 3
5

I assume you are talking about com.google.common.collect.ImmutableMap? According to the javadoc, that has been in guava since 2.0, which is really old (I can only find the year 2010 for version 3.0, so 2.0 is before that).

Map.of was only introduced with java 9, which was released in September 2017. That means that only the first option was available before java 9, but you needed a library.

Since this feature was finally integrated into java itself with java 9 and you don't need an external library anymore, I would argue Map.of is preferrable.

However, in terms of readability, I have always hated that Map.of as well as List.of return immutable implementations, which is not what an inexperienced (=has not fallen into that trap yet) developer expects.

If you are using guava anyways, ImmutableMap.of is the more readable option, but that's up to you.

🌐
Guava
guava.dev › releases › 23.0 › api › docs › com › google › common › collect › ImmutableMap.html
ImmutableMap (Guava: Google Core Libraries for Java 23.0 API)
Returns an immutable map containing the given entries, in order. ... public static <K,V> ImmutableMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
🌐
Java Code Geeks
javacodegeeks.com › home › core java
How to create immutable Map in Java - Java Code Geeks
March 17, 2020 - So considering the above definition of immutable, an immutable map is a map in which we can not insert, update or delete elements once it is created.This kind of Map will usually be required to have content which is not expected to be changed ...
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › › 9 › core › creating-immutable-lists-sets-and-maps.htm
3 Creating Immutable Lists, Sets, and Maps
The collection instances created by the Set.of, Map.of, and Map.ofEntries methods are the only ones whose iteration orders are randomized. The iteration ordering of collection implementations such as HashMap and HashSet is unchanged. The collections returned by the convenience factory methods added in JDK 9 are conventionally immutable.
🌐
Techie Delight
techiedelight.com › home › java › mutable, unmodifiable, and immutable empty map in java
Mutable, unmodifiable, and immutable empty Map in Java | Techie Delight
July 7, 2026 - 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.
🌐
How to do in Java
howtodoinjava.com › home › collections framework › how to create immutable map in java?
How to Create Immutable Map in Java?
July 1, 2024 - Starting with JDK 9, we can rely on the factory method Map.of() to create an immutable Map (with a maximum of 10 key-value pairs) [JEP-269]. The Map.of() is an overloaded method that accepts 0 to 10 key-value pairs.
Top answer
1 of 4
232

An unmodifiable map may still change. It is only a view on a modifiable map, and changes in the backing map will be visible through the unmodifiable map. The unmodifiable map only prevents modifications for those who only have the reference to the unmodifiable view:

Map<String, String> realMap = new HashMap<String, String>();
realMap.put("A", "B");

Map<String, String> unmodifiableMap = Collections.unmodifiableMap(realMap);

// This is not possible: It would throw an 
// UnsupportedOperationException
//unmodifiableMap.put("C", "D");

// This is still possible:
realMap.put("E", "F");

// The change in the "realMap" is now also visible
// in the "unmodifiableMap". So the unmodifiableMap
// has changed after it has been created.
unmodifiableMap.get("E"); // Will return "F". 

In contrast to that, the ImmutableMap of Guava is really immutable: It is a true copy of a given map, and nobody may modify this ImmutableMap in any way.

Update:

As pointed out in a comment, an immutable map can also be created with the standard API using

Map<String, String> immutableMap = 
    Collections.unmodifiableMap(new LinkedHashMap<String, String>(realMap)); 

This will create an unmodifiable view on a true copy of the given map, and thus nicely emulates the characteristics of the ImmutableMap without having to add the dependency to Guava.

2 of 4
11

Have a look at ImmutableMap JavaDoc: doc

There is information about that there:

Unlike Collections.unmodifiableMap(java.util.Map), which is a view of a separate map which can still change, an instance of ImmutableMap contains its own data and will never change. ImmutableMap is convenient for public static final maps ("constant maps") and also lets you easily make a "defensive copy" of a map provided to your class by a caller.

🌐
Guava
guava.dev › releases › 16.0 › api › docs › com › google › common › collect › ImmutableMap.html
ImmutableMap (Guava: Google Core Libraries for Java 16.0 API)
Returns an immutable map containing the given entries, in order. ... public static <K,V> ImmutableMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
🌐
Shariqsp
shariqsp.com › mobileTesting › ImmutableMap.html
ImmutableMap - Notes By ShariqSP
Compatibility: ImmutableMap.of can be used with older versions of Java (with Guava dependency), while Map.of requires Java 9 or higher.
🌐
Guava
guava.dev › releases › 7.0 › api › docs › com › google › common › collect › ImmutableMap.html
ImmutableMap (Guava: Google Core Libraries for Java - r07)
Returns an immutable map containing the given entries, in order. ... public static <K,V> ImmutableMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
🌐
Prutor
prutor.ai › prutor online academy › java › immutable map in java
Immutable Map in Java
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.
🌐
CodingTechRoom
codingtechroom.com › tutorial › java-java-immutable-maps-guide
Understanding and Creating Java Immutable Maps - CodingTechRoom
We will explore how to create immutable maps, utilize them effectively, and discuss their advantages and limitations. With the rise of multi-threaded applications, the necessity for immutable data structures has NEVER been greater. They mitigate issues related to shared mutability, making your application less prone to errors and more robust. ... Java 9 introduced the `Map.of` and `Map.ofEntries` methods for creating immutable maps.