was added in Java 1.4. It implements Map interface. But it doesn’t use equals() and hashCode() methods for comparing objects unlike other implementations of Map (e.g., HashMap).
Which is faster
operator for comparing objects, it is faster compared to
When to use
It is suitable where we need reference equality check instead of logical equality.
Any implementation of Map interface is supposed to use equals() to compare objects, but
doesn’t do that. So, it violates Map’s general contract. Hence it is easy to see that it is a special kind of Map which is rarely used.
IdentityHashMap hm = new IdentityHashMap();
Integer i1 = new Integer(10);
Integer i2 = new Integer(10);
THINGS TO TRY
- What happens if we replace
HashMap in the above program?
In that case,
i2 are duplicate keys because
i1.equals( i2 ) returns
true. As a result, the output would be:
In the above program, keys
are not duplicate because
i1 == i2