Javatpoint Logo
Javatpoint Logo

Immutable Map in Java

As 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:

  1. From existing Map using copyOf() function of Guava
  2. Using Collections.singletonMap()
  3. Using Builder() from ImmutableMap
  4. New ImmutableMap using of() function from Guava
  5. Creating an ImmutableMap from existing Map

Approach: From existing Map using copyOf() function of Guava

It 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 ImmutableMap

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.

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 Guava

An 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 Map

In 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:

  • It is safe for the threads to use.
  • They possess good memory.
  • They can be transferred to libraries run by third parties without any issues because they are unchangeable.

Disadvantages of Immutable Map:

  • The fact that immutable classes need a different object for every unique value is really their main drawback. These items can be expensive to create, particularly if they are large.
  • They also have limited flexibility in which updating of the contents may not be happen.
  • For creating an immutable map, we require a new instance also which involves the copy of the existing data that result in more memory usage and also slower performance.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA