Difference Between IdentityHashMap, WeakHashMap, and EnumMap in Java


The IdentityHashMap is a special type of hash class by which we handle the rare cases related to the reference-equality. This map compares the keys by using a " = = " operator where the normal hashmap uses the " equals " method for this. A Weak HashMap is a type of map interface where the hash table merges its keys with the weak reference type of values. This type of map class can not be used further just because of the lack of reference pointers. An enum map is a special type of map class which contains only the enum keys. This type of map is mainly used to inherit abstract classes with the same type of key values. The task is here to differentiate these map structures by considering various parameters.

Input

[ ARB, RDD, KOL,  DHKA ]

Output

Insertion Order of objects in IdentityHashMap : 
[ ARB, RDD, KOL, DHKA ]
Insertion Order of objects in WeakHashMap :
[ ARB, RDD, DHKA, KOL ]
Insertion Order of objects in EnumMap :
[ ARB, RDD, DHKA, KOL ]

Differences Between IdentityHashMap, WeakHashMap, and EnumMap

IdentityHashMap

WeakHashMap

EnumMap

In this we use the reference equality ( == operator ) with a same object memory.

It uses weak references for its keys with a automated element removal.

It can be implemented with the required keys as an instance class with an enum object.

Use System.identityHashCode () as a key base to filter the unique object.

A weak refered hash map can collect the garbage values at any time.

It follows the optimized performance with enum keys.

It has faster lookup with the equals () and hashCode () functions.

This process always use the dynamic keys with a map to insert some additional data into it.

The process is memory efficient to perform the data retrieval.

Here, the IdentityHashMap compares the reference values as per the key identity. Where the WeakHashMap give a passage to the keys to collect the garbage values. And the EnumMap structure is specialized to optimize the enum key values in a generic manner.

Methods used

Using the Synchronization at the Object level

Algorithm

In this algorithm we have declared some functions to perform the insertion order in an array list by considering a set value. Moreover, by performing an iteration with a loop we will set a time for the length traversal.

  • Step 1 − Start the process.

  • Step 2 − Declare input output stream.

  • Step 3 − Import the built-in classes and declared functions.

  • Step 4 − Declare a public class.

  • Step 5 − Set the functions.

  • Step 6 − Go for the insertion order.

  • Step 7 − Declare an array list and populate it.

  • Step 8 − Declare the Set values.

  • Step 9 − Print the values by following the insertion manner.

  • Step 10 − Declare a loop to iterate the process.

  • Step 11 − Set the timer value.

  • Step 12 − Run the process ang get the output value.

  • Step 13 − Terminate the process.

Syntax

In this syntax, we check a treeset first by populate them with some integer values. After that we have declared a map set to create some pairs of identity from those elements to filter them up as per their state by using the entrySet () and hasNext () functions on a table.

TreeSet < Integer > STTREE = new TreeSet <> () ;
STTREE . add ( 4 ) ;
STTREE . add ( 5 ) ;
STTREE . add ( 6 ) ;
STTREE . add ( 8 ) ;
STTREE . add ( 4 ) ;

IdentityHashMap < Integer, String > ihmap
= new IdentityHashMap < Integer, String > () ;
ihmap . put ( 10, "ARB" ) ;
ihmap . put ( 20, "RDD" ) ;
ihmap . put ( 30, "ARBRDD" ) ;
ihmap . put ( 40, "KOLDHKA" ) ;
ihmap . put ( 50, "You" ) ;

PRINT THE VALUE HERE ( " IdentityHashMap size : " + ihmap . size () ) ;
PRINT THE VALUE HERE ( " Initial identity hash map :  "     + ihmap ) ;

Hashtable < Integer, String > table  
   = new Hashtable < Integer, String > () ;
table . put ( 1 , "X" ) ;
table . put ( 2 , "Y" ) ;
table . put ( 3,  "Z" ) ; 
table . put ( 4,  "A" ) ;

for ( Map . Entry m : table . entrySet () )
   Iterator < IdentityHashMap . Entry < Integer, String > >
   itr = ihmap . entrySet () . iterator () ;
while ( itr . hasNext () ) {
   IdentityHashMap . Entry < Integer, String > entry
      = itr . next ( ) ;
   TreeMap < Integer,Integer > MAPTREE 
      = new TreeMap <> () ;
   MAPTREE . put ( 2,5 ) ;
   MAPTREE . put ( 3,6 ) ;
   MAPTREE . put ( 4,6 ) ;
   MAPTREE . put ( 2,3 ) ;
}

Using the ConcurrentModificationException level method

In this approach we are going to implement the ConcurrentModificationException to perform the various operations on these specific maps. ConcurrentModificationException is a fail fast operation, used to modify the iterated values without the user permission.

Example

In the example, we have input some data elements as integers and string to synchronize them up into a map by using the getKey () and getValue () functions for a IdentityHashMap. Moreover, we have iterated the inputs and updated them by using the ConcurrentModificationException.

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
import java.util.*;
public class ARBRDD{
   public static void main ( String [] args ){
      IdentityHashMap < Integer, String > ihmap
         = new IdentityHashMap <Integer, String> () ;
      ihmap . put ( 10, "ARB" ) ;
      ihmap . put ( 20, "RDD" ) ;
      ihmap . put ( 30, "4" ) ;
      ihmap . put( 40, "ARBRDD" ) ;
      ihmap . put ( 50, "You" ) ;
      
      System.out.println ( " IdentityHashMap size : " + ihmap . size () ) ;
      System.out.println ( " Initial identity hash map: " + ihmap ) ;
      
      Iterator < IdentityHashMap . Entry < Integer, String > >
         itr = ihmap . entrySet () . iterator () ;
      while ( itr . hasNext () ){
         IdentityHashMap . Entry < Integer, String > entry = itr. next () ;
         System.out.println ( " Key = " + entry . getKey () + ", Value = " + entry . getValue () ) ;
      }
   }
}

Output

IdentityHashMap size : 5
Initial identity hash map: {30 =4, 10 =ARB, 40 =ARBRDD, 50 =You, 20 =RDD}
Key = 30, Value = 4
Key = 10, Value = ARB
Key = 40, Value = ARBRDD
Key = 50, Value = You
Key = 20, Value = RDD

Conclusion

In this article, we have performed various operations on the IdentityHashMap, WeakHashMap, and EnumMap by using Java Collections Framework to get the differences between them. After performing the successful operations on them you can experience the need of the exceptions into these maps. In these the EnumMap is the best structure which does not allow the null values into it.

Updated on: 02-Nov-2023

147 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements