LinkedHashMap

LinkedHashMap

It is exactly same as HashMap,except the following differences.

HashMap
LinkedHashMap
  • The underlying data structure is Hashtable.
  • Insertion order is not preserve.
  • Introduce in 1.2 version.
  • The underlying data structure is Hashtable and LinkedList.
  • Insertion order is preserve.
  • Introduce in 1.4 version.

In the above, if we are replacing HashMap LinkedHashMap, the following is output.

{ Narayana=100, Sunny=800, Anitha=200, Vighnesh=500}

i.e. insertion order is preserve.

Note:- LinkedHashSet & LinkedHashMap are best suitable to implement cache applications.

Identity HashMap

This is exactly same as HashMap, except the following difference. In the use of HashMap JVM always uses.equals() method to identify duplicate keys. But the in the case of IdentitfyHashMap, JVM uses==(equal) operator to identify duplicate keys.

 {
  HashMap m = new HashMap ();
   integer i1 = new integer (10);  
    integer i2 = new integer ( 10);
     m. put ( i1, "narayana");
      m. put (i2, "anitha");  
     System.out.println(t); // 10= sunny
    }
}

In this case JVM uses.equals() method to check duplicate keys. Hence i1.equals(i2) returns true these are duplicated. If we are replacing HashMap with identify HashMap then JVM uses “==” operator,instead of .equals() method.

i1==i2 returns false, and these are not duplicated. Hence both entries will be added to the Map.The output is [ 10= kalyan, 10= pavan].

Leave a Reply