Saturday, January 28, 2017

IdentityHashMap in Java was added in Java 1.4 but still it's one of those lesser known class 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 object unlike other implementation of Map e.g. HashMap. Instead, IdentityHashMap uses equality operator "=="to compare keys and values in Java which makes it faster compare 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 general contract of Map interface which mandates using equals method for comparing Object. Also, IdentityHashMap vs HashMapis a good Java question and have 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 IdentityHashMap and HashMap

Though both HashMap and IdentityHashMap implements Map interface, have fail-fast Iterator and non-synchronized collections, 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.

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 is 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

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

importjava.util.IdentityHashMap;

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

//size of identityMap should be 2 here because two strings are different objectsSystem.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 objectSystem.out.println("Size of IdentityHashMap: " + identityMap.size());System.out.println("IdentityHashMap: " + identityMap);

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 usedto maintain as a proxy object.

6 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?

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?