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].

The Author

Narayanaswamy

Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in IT industry more than 7 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting and Website Development. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

Leave a Reply