IdentityHashMap in Java was added in Java 1.4 but still its one of those lessor known class in Java. 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 use 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 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 HashMap is a good Java question and have been asked couple of times. 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 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) 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 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 key difference between HashMap and IdentityHashMap in comparing Objects. IdentityHashMap uses equality operator for comparison i instead of equals method in Java :
That’s all on difference between IdentityHashMap and HashMap in Java. As I said IdentityHashMap violates Map interface general contract and should only be used when reference equality make sense. As per Javadoc, IdentityHashMap is suitable to keep object reference during Serialization and deep copy and can also be used to maintain as proxy object.
Other useful Java collection tutorials from Javarevisited Blog