Immutable Map in JavaAs the name implies, an immutable map type is called an immutable map. It indicates that the map's contents are read-only after declaration and are fixed or constant thereafter. Any attempt to modify, add, or remove components from the map results in the throwing of an UnsupportedOperationException. Null elements are not permitted in an immutable map either. A NullPointerException is triggered if an immutable map with a null element is attempted to be created. An UnsupportedOperationException is raised whenever an attempt is made to include a null entry in the map. Note: Since it is an immutable collection rather than a collection of immutable objects, any changes made to its contents are possible.Class Declaration:Class hierarchy:In order to create an Immutable Map, there are several ways to generate. These consist of:
Approach: From existing Map using copyOf() function of GuavaIt appears that you want to use Guava's copyOf() function to make a copy of an already-existing map. A Java library called Guava offers a number of utility classes for handling collections and other typical programming tasks. Implementation:FileName: MappingExample1.java Output: {1=Welcome, 2=to, 3=the, 4=World} Approach: Using Collections.singletonMap()The Collections. The singletonMap()()ethod returns an immutable map containing only one mapping. One key-value pair can be created with it to form a map. Implementation:FileName: MappingExample2.java Output: {John Roll No:=104} Approach: Using Builder() from ImmutableMapThe Builder() function in Guava is provided by the ImmutableMap class. This function allows you to generate an ImmutableMap from an existing Map, create a new ImmutableMap, or do both. Using ImmutableMap's Builder() The Builder() function in Guava is provided by the ImmutableMap class. This function allows you to generate an ImmutableMap from an existing Map, create a new ImmutableMap, or do both. Implementation:FileName: MappingExample4.java Output: {1=Welcome, 2=to, 3=the, 4=World} Approach: New ImmutableMap using of() function from GuavaAn Immutable Map can be created with Guava's of() function. You can give this method a set of key-value pairs to initialise the map.ImmutableMap.of() is overloaded in order to handle different numbers of elements (up to 5). If you need to create a map with more than five components, you can use ImmutableMap.builder() to generate the map. Implementation:FileName: MappingExample5.java Output: {1=Welcome, 2=to, 3=the, 4=World} Approach: Creating an ImmutableMap from existing MapIn Guava, we may change an existing map into an immutable map by using the copyOf() function. Here, we start by creating an existingMap, a basic HashMap with two key-value pairs. Next, we use ImmutableMap to create an immutable replica of the map.copyOf(previousMap). ImmutableMap is employed here. Since ImmutableMapcopyOf() creates a copy of the original map, modifications made to the original map after the ImmutableMap is formed will not affect the immutable version. Implementation:FileName: MappingExample6.java Output: {1=Welcome, 2=to, 3=the, 4=World} Advantages of Immutable Map:
Disadvantages of Immutable Map:
Next TopicK-4 City Program in Java
|