Using the IdentityHashMap in Java

In this article, we’ll discuss IdentityHashMap from the java.util package.

What Will We Learn?

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

Learn Java Collections Framework in-depth at the Java Collections Framework Tutorial.

1. IdentityHashMap Class Overview

This class is not a general-purpose Map implementation! While this class implements the Mapinterface, 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 HashTablebased implementation of the 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() — Removes all of the mappings from this map.
  • Object clone() — Returns a shallow copy of this identity hash map: the keys and values themselves are not cloned.
  • boolean containsKey(Object key) — Tests whether the specified object reference is key in this identity hash map.
  •  boolean containsValue(Object value)  — Tests whether the specified object reference is a value in this identity hash map.
  •  Set<Map.Entry<K,V>>entrySet()  — Returns a Set view of the mappings contained in this map.
  •  boolean equals(Object o) — Compares the specified object with this map for equality.
  •  void forEach(BiConsumer<? super K,? superV> action)  — 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) — Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
  • int hashCode() — Returns the hash code value for this map.
  • boolean isEmpty() — Returns true if this identity hash map contains no key-value mappings.
  •  Set keySet() — Returns an identity-based set view of the keys contained in this map.
  •  V put(K key, V value) — Associates the specified value with the specified key in this identity hash map.
  •  void putAll(Map<? extends K,? extends V> m) — Copies all of the mappings from the specified map to this map.
  •  V remove(Object key) — Removes the mapping for this key from this map if present.
  •  void replaceAll(BiFunction<? super K,? super V,? extends V> function)  —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() — Returns the number of key-value mappings in this identity hash map.
  •  Collection values() — 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 the Map Interface. Normal HashMap compares keys using the .equals method. But Identity HashMap compares its keys using the == operator. Hence, ‘a’ and new String(‘a’) are considered as two different keys. The initial size of Identity HashMap is 21, while the initial size of a 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");
 }
}
Output:
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

Further Learning

Collections Framework – EnumMap
Collections Framework – IdentityHashMap

Collections Framework – CopyOnWriteArraySet
Collections Framework – EnumSet

Collections Framework – CopyOnWriteArrayList

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s