In Java 9 there are some features are added in Java language and factory method for immutable map is one of them.
Characteristics of immutable map:
- As the name suggest these map are immutable.
- If any attempt made to add, delete and update elements in the map we will have UnsupportedOperationException.
- Immutable map do not allow null element either.
- If any attempt made to create a immutable map with null element, we will have NullPointerException. If any attempt made to add null element in map, we will have UnsupportedOperationException.
Creating immutable map in Java 8
To create immutable map in java 8, we use java.util.unmodifiableMap(Map map) method. unmodifiableMap(Map map): This method returns an unmodifiable view of the specified map. This method allows modules to provide users with “read-only” access to internal maps.
Syntax: public static Map unmodifiableMap(Map map) Returns: an unmodifiable view of the specified map. Exception: NA
Java code for immutable empty and no-empty map:
Above code will generate exception, because we are trying to add key-value pair in immutable Map.
Runtime Error : Exception in thread "main" java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableMap.put(Collections.java:1457) at ImmutableListDemo.main(File.java:17)
Creating immutable Map in java 9
To create immutable map in java 9, we use of() and ofEntries() method.
Java code to create immutable map in java 9:
After running above code, we will have UnsupportedOperationException.
Java code to create immutable map using Map.ofEntries() method in Java 9:
So of() and ofEntries() are method used to create immutable map in java 9.
This article is contributed by Abhishek Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Factory method to create Immutable List in Java SE 9
- Factory method to create Immutable Set in Java 9
- Immutable Map in Java
- How to create Immutable class in Java?
- Initialize a static Map using Java 9 Map.of()
- Primitive Wrapper Classes are Immutable in Java
- Immutable List in Java
- Immutable Set in Java
- Java: String is Immutable. What exactly is the meaning?
- Map Values() Method in Java With Examples
- EnumMap putAll(map) Method in Java
- Map clear() method in Java with Example
- Map containsKey() method in Java with Examples
- Map containsValue() method in Java with Examples
- Map get() method in Java with Examples
- Map entrySet() method in Java with Examples
- Map equals() method in Java with Examples
- Map putAll() Method in Java with Examples
- Map put() Method in Java with Examples
- Map isEmpty() Method in Java with Examples