IdentityHashMap and HashMap In Java

1
1517

What is IdentityHashMap

It is one of the lesser known class of Java is a part of Collection Framework which extends the [symple_highlight color=”blue”]AbstractMap[/symple_highlight] and implements the Map. IdentityHashMap class is almost similar to [symple_highlight color=”blue”]HashMap [/symple_highlight]with difference that is checks duplicate keys only on basis of reference value. In simple words it uses reference equality(==) instead of object equality (equals()) when comparing keys and values. In Map, duplicate key is checked by overriding Hash Code and equals but IdentityHashMap differs in this context, it intentionally breaks the contract of [symple_highlight color=”blue”]Map [/symple_highlight]which mandates the use of the equal’s method when comparing objects. This class provides all of the optional Map operations and permits null key and value.

IdentityHashMap obtains hash codes via Systems’ static int indentityHashCode (Object x) method instead of via each keys’ hashCode() method. The hash code for the null reference is zero. So in an IdentityHashMap, two keys K1 and K2 are considered equal if and only if (K1==K2).

IdentityHashMap
IdentityHashMap

When to use IdentityHashMap

This class is designed for use only in the rare cases where in reference-equality semantics are required. As IdentityHashMap use equality operator ’==’ to compare keys and values in Java which makes it faster compare to HashMap and suitable where you need reference equality check and instead of logical equality. IdentityHashMap has one tuning parameter (which affects performance but not semantics): expected maximum size. This parameter is the maximum number of key-value mappings that the map is expected to hold.
IdentityHashMap (int expectedMaxSize)

A typical use of this class is topology-preserving object graph transformations, such as serialization and deep copying

Difference between IdentityHashMap and HashMap

Both IdentityHashMap & HashMap class implements MAP interface but below are the few key difference in both cases :

1) IdentityHashMap uses reference equality while HashMap use object equality
2) Unlike HashMap which uses hashcode() method to find bucket location, IdentityHashMap doesn’t use hashcode() method instead it uses system.identityHashCode() to find bucket location.
3) IdentityHashMap will yield better performance than HashMap
4) Immutability of key- To safely store the objet in HashMap keys must be immutable; identityhashmap doesn’t required keys to be immutable as it is not relied on equals and hashcode.
5) Initial capacity of HashMap is 16 by default. Initial capacity of IdentityHashMap is 21 by default.

Similarities between IdentityHashMap and HashMap

The similarities among them are they allow null Key Value, are non-synchronized, both are HashTable based implementation and both classes iterator are fail-fast iterator.

Example / Sample Code for IdentityHashMap

package com.example.samplecode;

import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Mapl

public class IdenditityHashMapExample{

  public static void main(String [] args){

    Map identityHM = new IdentityHashMap();
    Map hashM = new HashMap();

    identityHM.put("goyalsbit","value1");
    identityHM.put(new String("goyalsbit"),"value2");
    identityHM.put("goyalsbit","value3");

    System.out.println("identityHM size::"+ identityHM.keySet.size()+"identityHM content::"+ identityHM);

    hashM.put("goyalsbit","value1");
    hashM.put( new  String("goyalsbit"),"value2");
    hashM.put("goyalsbit","value3");

    System.out.println("hashM size::"+ hashM.keySet.size()+"hashM content::"+ hashM);

}

OUTPUT

identityHM size::2 identityHM content::{goyalsbit=value2, goyalsbit=value3}
hashM size::1 hashM content::{ goyalsbit=value3}

The key size of the IdentityHashMap is 2 because there the goyalsbit and new String (“goyalsbit”) are considered two different Object. The comparison is done using == operator.
For HashMap the keySize is 1 because k1.equals(k2) returns true for all three keys and hence it keep on removing the old value and updating it with the new one.

References

https://docs.oracle.com/javase/7/docs/api/java/util/IdentityHashMap.html

Previous articleMobile Ecosystem
Next articleManaging Business Change
Rahul Goel has 8+ years of experience in IT industry working on design & development of web based applications in Java /JEE technology stack. He posses various business domains knowledge like HealthCare, Banking & Finance, E Commerce, Retail, and Security. Rahul is passionate about cricket, traveling and his technical writing.

1 COMMENT

LEAVE A REPLY