IdentityHashMap



           IdentityHashMap is one of the important member of Map family. It had been introduced in jdk 1.4 . But this data structure could not get that much of popularity as that of HashMap . This Hashtable based data structure also implements Map , Serializable, Cloneable and extends AbstractMap class. As per Oracle documentation
"This class is not a general-purpose Map implementation! While this class implements the Map interface, it 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 the rare cases wherein reference-equality semantics are required. "

          To be precise this is a special implementation of Map interface which uses reference-equality instead of well known logical-equality to compare keys , i.e. , it does not use equal() and hashCode() method of object . It uses "==" operator to compare objects . This implementation makes IdentityHashMap faster than HashMap.

          IdentityHashMap is useful where logical equality is not required . This kind of Map is required at the time of deep copy of objects or serialization of objects where we need to maintain a table to keep tack of the already processed objects as in this kind of operation reference equality is required to check processed objects .


Properties of IdentityHashMap :

  • Key property of IdentityHashMap is that it uses reference- equality of objects. So it uses "==" operator instead of equal() method .
  • As it uses "==" operator to check reference equality , IdentityHashMap is faster than HashMap which uses equal() method to check logical-equality.
  • IdentityHashMap uses public static native int identityHashCode(Object paramObject) method of System class ( System.identityHashCode(paramObject) ) for basic operations ( put and get ) to identify consistent hash code and bucket location for objects.
  • IdentityHashMap is not thread safe , i.e. , synchronized . To make it synchronized programmer can wrap the Map using Collections.synchronizedMap . But it is recommended to initialize the Map as synchronized on .
      Map map = Collections.synchronizedMap(new IdentityHashMap<>());
  • IdentityHashMap uses one tuning parameter MAXIMUM_CAPACITY which defines the no of maximum Key-value pair can be mapped in bucket location .
      private static final int MAXIMUM_CAPACITY = 536870912;

            This is implemented to increase iteration performance of the Map . As per Oracle documentation if the size of the map (the number of key-value mappings) sufficiently exceeds the expected maximum size, the number of buckets is increased Increasing the number of buckets ("rehashing") may be fairly expensive, so it pays to create identity hash maps with a sufficiently large expected maximum size. On the other hand, iteration over collection views requires time proportional to the number of buckets in the hash table, so it pays not to set the expected maximum size too high if you are especially concerned with iteration performance or memory usage.


Differences between IdentityHashMap and HashMap :

          Though IdentityHashMap and HashMap are from same Map family they have lots of similarities as well as differences . Main common point is that both implements Map interface and hash table based implementation . Both of these classes are non synchronized data structure . Both implements fail-fast Iterator to iterate the structure.
Apart from all these similarities there are few dissimilarities :
  • Key difference is internal implementation of both class. IdentityHashMap uses "==" operator to check reference equality of key and value where HashMap uses equal() method of object to check logical or object equality of key and value .
  • IdentityHashMap is faster than HashMap as it does not use equal() and hashCode() method of Object.
  • IdentityHashMap uses System.identityHashCode method to find bucket location of the key where HashMap uses hashCode method of Object to find bucket location of key.
  • It is recommended to use immutable objects as key in HashMap to avoid collision and for faster performance . But for IdentityHashMap immutability of objects is not required as it does not rely on equal() and hashCode() methods.

Comments

Popular posts from this blog

HashMap

What is Stream API in Java

HashMap internal working principle