Uncovering key differences between IdentityHashMap and HashMap in Java

As a concurrent, object-oriented computer programming language, Java web development has been incredibly embraced by web programmers all over the world. With innovative features and functionalities getting added to Java on a regular basis, it won't be wrong to say that the software product is definitely here to stay. Being a class-based programming language, Java comprises of several classes that are being worked upon in the best possible way. This article of mine focuses on two such fabulous Java classes that have served as handy options for implementing numerous Java web development projects. Here, I'm going to discuss about HashMap and IdentityHashMap along with the main differences between them.

Firstly, let's get the vibe of IdentityHashMap

Added in Java 1.4 version, IdentityHashMap hasn't been much recognized by every Java developer. As a special implementation of Map interface, IdentityHashMap doesn't use either the hashCode() or the equals() method for comparing multiple objects used in Java. Rather, IdentityHashMap uses the equality operator “==” for comparing multiple keys and values that are used in Java. Here is the code snippet involved with the use of equality operator(==) for comparing multiple keys:

import java.util.HashMap;
 import java.util.IdentityHashMap;
 import java.util.Map;
public class IdentityMapDemo {
public static void main(String[] args) {
 Map identityMap = new IdentityHashMap();
 Map hashMap = new HashMap();
 identityMap.put("a", 2);
 identityMap.put(new String("a"), 1);
 identityMap.put("a", 2);
hashMap.put("a", 2);
 hashMap.put(new String("a"), 1);
 hashMap.put("a", 2);
System.out.println("Identity Map KeySet Size :: " +  identityMap.keySet().size());
 System.out.println("Hash Map KeySet Size :: " + hashMap.keySet().size());

Now, let's dig deep into the usage of HashMap in Java Unlike the IdentityHashMap class, the HashMap class uses the equals method for comparing keys and values. Also, HashMap uses the hashcode for finding bucket location. Moreover, HashMap is a bit slow because it uses the not-so-happening equals() and hashCode() method. Immutability is yet another key point of difference between HashMap and IdentityHashMap. While HashMap requires keys for safely storing objects, it is mandatory for such keys to be completely immutable. On the contrary, IdentityHashMap doesn't require any such keys to be immutable simply because it doesn't rely on the usage of hashCode and equals() method. Here is the way in which the equals() method is used in HashMap for determining the uniqueness of the Key:

k1.equals(k2) On running the above mentioned code, the result which is obtained is shown below: Identity Map KeySet Size :: 2 Hash Map KeySet Size :: 1

If you carefully observe the above code, you'll find that the Keysize of Identity Map has been kept as 2 simply because a and new String(“a”) are being regarded as two individual Objects. Yet another important thing to note is that the keySize has been kept as 1 simply because K1.equals(K2) is a function that returns true for all the three types of Keys. This plays a vital role in removing old values and updating the same with the new ones.


So these were the main differences between IdentityHashMap and HashMap. Although both these maps behave in quite a similar manner when it comes to using them for Keys, I'm sure after reading this article, you'd have learnt a lot about things that make these two maps different from each other.

Celin Smith Celin Smith is Java developer and blogger at Xicom who loves to write about web & mobile apps. Xicom is a one of the leading Java Development Outsourcing Company which provides different array of genuine software solutions like Custom Software Development, IT Outsourcing Services, Web application development services to its clients globally. Xicom Technologies Ltd., a software development realm, is a CMMI Level-3 & ISO 9001 Certified company that is renowned for providing top-notch services to its global clientele.
  • java application development
  • java web application development
  • java software development
By Celin Smith On 26 Dec, 14  Viewed: 214

Other blogs you may like

Do the Arguments in JavaScript Are Passed By Value Or Reference?

Since Java is a complicated language, beginners may find developing [Java Web Application Development][1] or web pages a difficult task. JavaScript (also referred to as JS) is much easier compared to Java, and helps to enhance how your web page works. Incorporating JS into your site's web page... By Celin Smith   On 23 Dec 2014  Viewed: 202