IdentityHashMap in Java

In this article, we’ll discuss IdentityHashMap from the collections framework with examples.

What will we Learn?

  1. IdentityHashMap Class Overview
  2. IdentityHashMap Class Constructors
  3. IdentityHashMap Class Methods
  4. IdentityHashMap Class Example

1. IdentityHashMap Class Overview

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.
IdentityHashMap is a HashTable based implementation of Map Interface. Normal HashMap compares keys using '.equals' method. But Identity HashMap compares its keys using '==' operator.
Note that this implementation is not synchronized. If multiple threads access an identity hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. 
For Example:
   Map m = Collections.synchronizedMap(new IdentityHashMap(...));

2. IdentityHashMap Class Constructors

  • IdentityHashMap() - Constructs a new, empty identity hash map with a default expected maximum size (21).
  • IdentityHashMap(int expectedMaxSize) - Constructs a new, empty map with the specified expected maximum size.
  • IdentityHashMap(Map<? extends K,? extends V> m) - Constructs a new identity hash map containing the key-value mappings in the specified map.

3. IdentityHashMap Class Methods

  • void clear() - This method removes all of the mappings from this map.
  • Object clone() - This method returns a shallow copy of this identity hash map: the keys and values themselves are not cloned.
  • boolean containsKey(Object key) - This method tests whether the specified object reference is a key in this identity hash map.
  • boolean containsValue(Object value) - This method tests whether the specified object reference is a value in this identity hash map.
  • Set<Map.Entry<K,V>> entrySet() - This method returns a Set view of the mappings contained in this map.
  • boolean equals(Object o) - This method compares the specified object with this map for equality.
  • void forEach(BiConsumer<? super K,? super V> action) - This method performs the given action for each entry in this map until all entries have been processed or the action throws an exception. 
  • V get(Object key) - This method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
  • int hashCode() - This method returns the hash code value for this map.
  • boolean isEmpty() - This method returns true if this identity hash map contains no key-value mappings.
  • Set keySet() - This method returns an identity-based set view of the keys contained in this map.
  • V put(K key, V value) - This method associates the specified value with the specified key in this identity hash map.
  • void putAll(Map<? extends K,? extends V> m) - This method copies all of the mappings from the specified map to this map.
  • V remove(Object key) - This method removes the mapping for this key from this map if present.
  • void replaceAll(BiFunction<? super K,? super V,? extends V> function) - This method replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
  • int size() - This method returns the number of key-value mappings in this identity hash map.
  • Collection values() - This method returns a Collection view of the values contained in this map.

4. IdentityHashMap Class Example

As we know that IdentityHashMap is a HashTable based implementation of Map Interface. Normal HashMap compares keys using '.equals' method. But Identity HashMap  compares its keys using '==' operator. Hence 'a' and new String('a') are considered as 2 different keys. The initial size of Identity HashMap is 21 while the initial size of normal HashMap is 16.
import java.util.IdentityHashMap;

public class IdentityHashMapExample {
 public static void main(final String[] args) {
   final IdentityHashMap<String, String> identityHashMap = new IdentityHashMap<String, String>();

         identityHashMap.put("a", "Java");
         identityHashMap.put(new String("a"), "J2EE");
         identityHashMap.put("b", "J2SE");
         identityHashMap.put(new String("b"), "Spring");
         identityHashMap.put("c", "Hibernate");

         for (final String str : identityHashMap.keySet()) {
             System.out.println("Key : " + str + " and Value : " + identityHashMap.get(str));

         System.out.println("Size of map is : " + identityHashMap.size());
         System.out.println("Here 'a' and new String('a') are considered as separate keys");
Key : a and Value : Java
Key : b and Value : J2SE
Key : c and Value : Hibernate
Key : b and Value : Spring
Key : a and Value : J2EE
Size of map is : 5
Here 'a' and new String('a') are considered as separate keys


Related Collections Examples

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course