Difference between IdentityHashMap and HashMap in Java

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.
Read more »

0 Response to "Difference between IdentityHashMap and HashMap in Java"

Posting Komentar