Thursday, August 18, 2022

Difference between HashMap and IdentityHashMap in Java? Example

Hello guys, if you are wondering what is differnece between an HashMap and IdentiyHashMap in Java then you are at right place. IdentityHashMap in Java was added in Java 1.4 but still, it's one of those lesser-known classes in Java. The main difference between IdentityHashMap and HashMap in Java is that IdentityHashMap is a special implementation of Map interface which doesn't use equals() and hashCode() method for comparing objects unlike other implementations of Map e.g. HashMap. Instead, IdentityHashMap uses the equality operator "=="  to compare keys and values in Java which makes it faster compared to HashMap and suitable where you need reference equality check and instead of logical equality.


By the way, IdentityHashMap is a special implementation of Map interface much like EnumMap but it also violates the general contract of Map interface which mandates using equals method for comparing Object. Also, IdentityHashMap vs HashMap is a good Java question and has been asked a couple of times.  

Even though this question is not as popular as How HashMap works in Java or Difference between Hashtable and HashMap, it’s still a good question to ask. In this Java tutorial, we will see an example of IdentityHashMap and explores some key differences between IdentityHashMap and HashMap in Java.

Difference between HashMap and IdentityHashMap in Java? Example



Difference between IdentityHashMap and HashMap in Java

Though both HashMap and IdentityHashMap implements the Map interface, have fail-fast Iterator and non-synchronized collections, the following are some key differences between HashMap and IdentityHashMap in Java.

1. The main difference between HashMap vs IdentityHashMap is that IdentityHashMap uses equality operator "==" for comparing keys and values inside Map while HashMap uses equals method for comparing keys and values.

2. Unlike HashMap, who uses hashcode to find bucket location, IdentityHashMap also doesn't use hashCode() instead it uses System.identityHashCode(object).

3) Another key difference between IdentityHashMap and HashMap in Java is Speed. Since IdentityHashMap doesn't use equals() its comparatively faster than HashMap for object with expensive equals() and hashCode().

4) One more difference between HashMap and IdentityHashMap is Immutability of the key. One of the basic requirement to safely store Objects in HashMap is keys needs to be immutable, IdentityHashMap doesn't require keys to be immutable as it has not relied on equals and hashCode.

There is also a class called IdentityHashtable which is analogous to Hashtable in Java but it’s not part of standard JDK and available in com.sun... package.






Example of IdentityHashMap in Java

Difference between IdentityHashmap vs HashMap in Java Interview questionHere is an example of IdentityHashMap in Java which shows the key difference between HashMap and IdentityHashMap in comparing Objects.  IdentityHashMap uses equality operator for comparison instead of equals method in Java :

import java.util.IdentityHashMap;

/**
 * Java program to show difference between HashMap and IdentityHashMap in Java
 * @author Javin Paul
 */

public abstract class Testing {

   
public static void main(String args[]) {
        IdentityHashMap
<String, String> identityMap = new IdentityHashMap<String, String>();
     
        identityMap.
put("sony", "bravia");
        identityMap.
put(new String("sony"), "mobile");
     
       
//size of identityMap should be 2 here because two strings are different objects
        System.
out.println("Size of IdentityHashMap: " + identityMap.size());
        System.
out.println("IdentityHashMap: " + identityMap);
     
        identityMap.
put("sony", "videogame");
     
         
//size of identityMap still should be 2 because "sony" and "sony" is same object
        System.
out.println("Size of IdentityHashMap: " + identityMap.size());
        System.
out.println("IdentityHashMap: " + identityMap);
   
   
}
}

Output
Size of IdentityHashMap:
2
IdentityHashMap:
{sony=bravia, sony=mobile}
Size of IdentityHashMap:
2
IdentityHashMap:
{sony=videogame, sony=mobile}


That’s all on the difference between IdentityHashMap and HashMap in Java.  As I said IdentityHashMap violates Map interface general contract and should only be used when reference equality makes sense. As per Javadoc, IdentityHashMap is suitable to keep object reference during Serialization and deep copy and can also be used to maintain as a proxy object.  You can also see these Java Collection Courses to learn more about essential Collection classes in Java


Other useful Java collection tutorials from Javarevisited Blog

7 comments :

Anonymous said...

Hi, how about performance? I assume IdentityHashMap will perform better than HashMap because of reference comparison but how much performance gain you make when you switch from HashMap to IdentityHashMap?

Anonymous said...

You will never have hash collison with IdentityHashMap :)

Anonymous said...

What is meany by hash collision

Anonymous said...

What is hash collision?

Json said...

What is the real life example where you want to use IdentityHashMap? I know that all meaningful object override equals() and hashCode but I think for things like database Connections, or file handles we can use this map as cache. agree?

Unknown said...

Article is great to read !
I guess, few corrections required in terms of language used.

"...Instead, IdentityHashMap uses equality operator "==" to compare keys and values in Java which..."

Map do not compare Values, it only compares Keys. You should correct it where ever it is used.

Sandeep said...

Good explanation!

Post a Comment