IdentityHashMap

Let's discuss IdentityHashMap.

IdentityHashMap is another type of Map that implements Map, Serializable, and Cloneable interfaces and extends the AbstractMap class. The main feature of this map is that while storing elements, the equality of the keys is checked on the basis of reference instead of the equals method. What this means is that if we have two keys, key1 and key2, then key1 will be considered equal to key2 if key1 == key2. In other words, if both the keys reference the same object.

This means that IdentityHashMap intentionally violates Map’s general contract which mandates the use of the equals method when comparing objects. This class is designed for use only in rare cases wherein reference-equality semantics are required.

Some of the features of IdentityHashMap are:

  1. The IdentityHashMap stores the elements in random order.
  2. The IdentityHashMap allows a single null key.
  3. The IdentityHashMap is not thread-safe.

Difference between HashMap and IdentityHashMap

Let’s discuss some of the differences between a HashMap and IdentityHashMap.

  1. IdentityHashMap uses reference equality to compare keys and values while HashMap uses object equality to compare keys and values.
  2. IdentityHashMap does not use the hashCode() method. Instead it uses System.identityHashCode() to find the bucket location.
  3. IdentityHashMap does not require keys to be immutable as it does not rely on the equals() and hashCode() methods. To safely store the object in HashMap, keys must be immutable.
  4. The default initial capacity of HashMap is 16; whereas, for IdentityHashMap, it is 32.

These are the major differences between a HashMap and IdentityHashMap. All the operations such as create, add, fetch, remove, etc in an IdentityHashMap are the same as these operations in hashMap. We will be looking at an example to understand the difference in how HashMap and IdentityHashMap run.

First, let’s create an Employee class that overrides both the equals() and the hashCode() method.

Get hands-on with 1200+ tech skills courses.