Difference between HashMap and IdentityHashMap in Java


HashMap and IdentityHashMap are the key value data sets which are used to access the key data pairs. More specifically.

A HashMap is a Java collection framework which provides the functionality of a proper hash table data set. The map stores the element value as a key or pairs which are the unique identifiers in nature. This map also permits the null values as a non synchronized class.

The IdentityHashMap is a special type of hash class by which we handle the rare cases related to the reference-equality. This map compare the keys by using a " = = " operator where the normal hash map uses " equals " method for this.

Though, there are some notable differences between these HashMaps we have tried to discuss here.

Input

[ ARB, RDD, KOL, DHKA ]

Output

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

Differences Between HashMap and IdentityHashMap

HashMap

IdentityHashMap

HashMap use a hash table to store the data.

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

Elements sorted into an arbitrary manner.

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

It works in a constant time frame.

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

Here we can see, the HashMap is an operation specific process which works on the element equality. On the other hand, the IdentityHashMap compares the key value in the first place by following the reference identity for the rare cases.

Methods Used

Perform the process by using the hasNext () method

Algorithm

In this algorithm we will set some functions to perform the element insertion into it. Further by declaring a set data structure we are going to use a loop for the length traversal with the timer.

  • 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

The syntax will check a treeset first with some integer values. After that we will declare a identity set to create some pairs from those elements as ( int, character ) to filter them up as per there values with the set iteration.

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 ( "IdentityHashMap size : " + ihmap . size () ) ;
PRINT THE VALUE ( "Initial identity hash map: " + ihmap ) ;
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 hasNext () Method

In this approach, we are going to use the hashNext () function to perform some specific operations on a HashMap and IdentityHashMap. A hasNext () is a function encoded by some scanner class which search for the true or false values in a record.

Example

In the example mentioned below, we have taken some inputs in the formation of an identity hashset to check the presence of some particular keys in a treeset.

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 > identity_hash = 
         new IdentityHashMap < Integer, String > () ;
      
      identity_hash . put ( 10, "ARB" ) ;
      identity_hash . put ( 15, "2022" ) ;  
      identity_hash . put ( 20, "RDD" ) ;
      identity_hash . put ( 25, "ARBRDD" ) ;
      identity_hash . put ( 30, "KOL" ) ;
      
      System.out.println ( "Initial Mappings are : " + identity_hash ) ;
      System.out.println ( "Is the value 'Kol' present? " +  identity_hash . containsValue ( "KOL" ) ) ;
      System.out.println ( "Is the value 'World' present? " + identity_hash . containsValue ( "World" ) ) ;
   }
}

Output

Initial Mappings are: {30 =KOL, 10 =ARB, 15= 2022, 25= ARBRDD, 20= RDD}
Is the value 'Kol' present? true
Is the value 'World' present? false

Conclusion

In this article today we have learned about the notable differences between a HashMap and an IdentityHashMap. The simple Hash uses the hashCode () function to find the locations of the brackets. Where else, the IdentityHashMap do not use this feature without chaining the elements.

Updated on: 02-Nov-2023

92 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements