IdentityHashMap is the only map in JDK useful for tracking objects by identity (object is identical only to itself) instead of tracking by equality (if
object1.equals( object2 ), they are equal).
IdentityHashMap is used mostly in cases when you can’t modify tracked objects definition by either adding additional fields or writing
Let’s see how some of these limitations could be removed:
If you can’t add
hashCodeto the class (or it has different implementations of these methods), it could be solved by Trove maps custom hashing strategies.
If there is a primary key field in the object – try to use an ordinary map with this field as a key instead of
IdentityHashMapwith object itself as a key.
System.identityHashCode to get object identity hash code – some semi-unique
int value identifying each object.
System.identityHashCode belongs to a set of Java intrinsic methods, which is a small set of core Java methods, which are directly (or nearly directly) mapped onto single (or just a few) CPU instructions.
You can find the list of Java 8 intrinsic methods in this listing. Look for
do_intrinsic macros in the second half of the file.