Factory method to create immutable Map in Java 9
Last Updated :
11 Dec, 2018
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:
Java
// Java code illustrating immutable map in java 8
import java.util.*;
class ImmutableListDemo
{
public static void main(String args[])
{
// empty map
Map<Integer, String> empty = new HashMap();
Map immutable1 = Collections.unmodifiableMap(empty);
// non-empty map
Map<Integer, String> non_empty = new HashMap();
non_empty.put(1, "ide");
Map immutable2 = Collections.unmodifiableMap(non_empty);
// adding key-value pair in these immutable map
immutable1.put(1,"gfg");
immutable2.put(2,"quiz");
}
}
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:
Java
// Java code illustrating of() method
import java.util.*;
class ImmutableListDemo
{
public static void main(String args[])
{
// empty immutable map
Map<Integer, String> immutable1 = Map.of();
// non-empty immutable map
Map<Integer, String> immutable2 = Map.of(1, "ide",2, "quiz");
// adding key-value pair in these immutable map
immutable1.put(1,"gfg");
immutable2.put(3,"contribute");
}
}
After running above code, we will have UnsupportedOperationException.
Java code to create immutable map using Map.ofEntries() method in Java 9:
Java
// Java code illustrating ofEntries method
import java.util.*;
import java.util.Map.Entry;
class ImmutableListDemo
{
public static void main(String args[])
{
// empty immutable map
Map<Integer, String> immutable = Map.ofEntries();
// non-empty immutable map
Map.Entry<Integer, String> entry1 = Map.entry(1, "gfg");
Map.Entry<Integer, String> entry2 = Map.entry(2, "contribute");
Map<Integer, String> immubatleMap = Map.ofEntries(entry1,
entry2);
}
}
So of() and ofEntries() are method used to create immutable map in java 9.
Similar Reads
Factory method to create Immutable Set in Java 9 Java 9 was released around march 2017 and please install jdk 9, it will be helpful in understanding the code used in this article. In Java 9, there are some features added in Java language and factory method for immutable Set is one of them. So lets get started! Characteristics of Immutable Set: As
3 min read
Factory method to create Immutable List in Java SE 9 Java 9 was released around march 2017 and In Java 9 there are some features are added in Java language and factory method for immutable List is one of them. Characteristics of Immutable List: As the name suggest these lists are immutable. If any attempt made to add, delete and update elements in the
3 min read
Map clear() method in Java with Example The java.util.Map.clear() method in Java is used to clear and remove all of the elements or mappings from a specified Map collection. Syntax: void clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate
2 min read
Collecting a Stream to an Immutable Collection in Java Streams and Collectors were introduced in Java 8 introduced the concept of Streams. A Stream is a sequence, a sequence of objects. We generate Streams from input sources like Arrays, Lists, etc., and support aggregation operations like filter, map, limit, reduce, etc. We use Streams to pipeline data
6 min read
EnumMap putAll(map) Method in Java The Java.util.EnumMap.putAll(map) method in Java is used to copy all the mappings from one map to a newer one. Older mappings are replaced in a newer one. Syntax: void putAll(map) Parameters: The method takes one parameter map. It is the map which is to be copied to the newer one. Return Value The m
2 min read
Methods to Create Preallocated HashMap in Java 19 Java 19 has introduced some new methods to create preallocated HashMaps which can enhance the performance of your application. In this article, we will explore the concept of preallocated HashMaps and how to create them using the new methods in Java 19. HashMap is a widely used data structure in Jav
3 min read