IdentityHashMap in Java
IdentityHashMap is a special Map implementation in Java where keys are compared using == instead of equals().
It belongs to the java.util package.
This means:
- Two keys are considered equal only if they refer to the same object in memory
- Even if two different objects have the same content, they are treated as different keys
What is IdentityHashMap?
An IdentityHashMap is a hash table–based Map implementation that:
- Uses reference equality (
==) for comparing keys - Uses System.identityHashCode() instead of
hashCode() - Allows null keys and null values
- Is not synchronized
- Is faster in some specialized cases (like object identity tracking)
Package
import java.util.IdentityHashMap;
Features of IdentityHashMap

1. Uses Reference Equality (==)
- Normal maps use
.equals()to compare keys. - IdentityHashMap checks if both keys are the same object, not if they have the same content.
2. Allows Duplicate Keys with Same Value
If two objects are different instances but contain the same data, they will be treated as different keys.
3. No Hash Collision Based on Content
Since .equals() and .hashCode() are ignored, key uniqueness = object identity.
4. Good for Framework / Low-Level Use
Used internally by:
- JVM
- Serialization code
- Dependency injection frameworks
- Object graph tracking
5. Not for Daily Use
Not recommended for regular business logic because behavior can be confusing.
Constructors of IdentityHashMap
1. Default Constructor
Creates an empty IdentityHashMap.
IdentityHashMap<K, V> map = new IdentityHashMap<>();
2. With Initial Capacity
Sets initial size for better performance.
IdentityHashMap<K, V> map = new IdentityHashMap<>(32);
3. From Another Map
Copies entries from another Map into IdentityHashMap.
IdentityHashMap<K, V> map = new IdentityHashMap<>(existingMap);
Common Methods of IdentityHashMap
IdentityHashMap implements the Map<K, V> interface, so it supports all standard Map methods — but behaves differently because keys are compared using == (reference equality).
| Method | Description |
|---|---|
| put() | Adds or updates a key-value pair. |
| get() | Returns value for the given key. |
| remove() | Removes the entry for the key. |
| containsKey() | Checks if the map contains the given key reference. |
| containsValue() | Checks if the map contains the given value. |
| size() | Returns the number of entries. |
| isEmpty() | Returns true if map contains no entries. |
| clear() | Removes all key-value pairs. |
| keySet() | Returns a Set of keys. |
| values() | Returns a Collection of all values. |
| entrySet() | Returns a Set of key-value entries. |
| putAll() | Copies all entries from another map. |
| clone() | Creates a shallow copy of the IdentityHashMap. |
| equals(Object o) | For IdentityHashMap, equals() compares entries normally (value-equal), but internal comparison still uses == for keys. |
| hashCode() | Returns a hash code of the map. (IdentityHashMap uses identityHashCode() internally.) |
Difference Between HashMap vs IdentityHashMap
| Feature | HashMap | IdentityHashMap |
|---|---|---|
| Key Comparison | Uses .equals() | Uses == (reference equality) |
| Key Uniqueness | Keys with same content considered same | Keys must be same object instance |
| Duplicate-like Keys | Not allowed | Allowed (if different objects) |
| Uses hashCode() | Yes | No (uses identity hash) |
| Performance | Slightly slower due to equals/hashCode | Faster key comparison |
| Use Case | General-purpose Map | Low-level, framework, object identity tracking |
| Allows null keys/values | Yes | Yes |
| Behavior with immutable keys | Normal | Can still treat same content as different keys |
| Thread Safety | Not thread-safe | Not thread-safe |
| Typical Use | Everyday coding | JVM internals, serialization, DI frameworks |
Simple Example Showing the Difference
HashMap
HashMap<String, Integer> map = new HashMap<>();
String a = new String("hello");
String b = new String("hello");
map.put(a, 1);
map.put(b, 2);
System.out.println(map.size()); // 1
HashMap treats both keys as same because a.equals(b) is true.
IdentityHashMap
IdentityHashMap<String, Integer> map = new IdentityHashMap<>();
String a = new String("hello");
String b = new String("hello");
map.put(a, 1);
map.put(b, 2);
System.out.println(map.size()); // 2
IdentityHashMap treats them as different because a == b is false.
Why Use IdentityHashMap?
Use it when identity-based comparison is required instead of value comparison. Examples:
- Object-graph processing
- Serialization frameworks
- Tracking visited objects
- Caching “object identity” instead of value
- Storing metadata about object references
IdentityHashMap Constructors
IdentityHashMap<K, V> map = new IdentityHashMap<>();
IdentityHashMap<K, V> map = new IdentityHashMap<>(50); // with capacity
IdentityHashMap<K, V> map = new IdentityHashMap<>(anotherMap);
Example: How IdentityHashMap Works
import java.util.IdentityHashMap;
public class IdentityExample {
public static void main(String[] args) {
IdentityHashMap<String, String> map = new IdentityHashMap<>();
String key1 = new String("A");
String key2 = new String("A"); // same content, different object
map.put(key1, "Apple");
map.put(key2, "Ant"); // treated as different key
System.out.println(map);
System.out.println("Size: " + map.size());
}
}
Output:
{A=Apple, A=Ant}
Size: 2
Even though both keys have the same content, they are stored as different keys because:
key1 == key2 → false
key1.equals(key2) → true
IdentityHashMap uses the first one (reference equality).
Example with Integer Objects
Integer a = 1000;
Integer b = 1000;
IdentityHashMap<Integer, String> map = new IdentityHashMap<>();
map.put(a, "Hello");
map.put(b, "World");
System.out.println(map.size());
Output:
2
Because a and b are two different objects in memory.
Time Complexity
| Operation | Complexity |
|---|---|
| put() | O(1) |
| get() | O(1) |
| remove() | O(1) |
| iteration | O(n) |
Advantages
- Faster for identity-based key lookup
- Good for object-graph traversal
- Can store duplicate “value-equal” keys
Disadvantages
- Not for general use
- Confusing behavior for beginners
- Not thread-safe
- Keys must be compared only by reference
- No predictable iteration order
When NOT to Use IdentityHashMap
- When you expect normal behavior like HashMap
- When keys should be compared using value equality
- When storing strings or wrapper objects (Integer, Double, etc.)
- In multi-threaded environments (without external sync)
Points to Remember
IdentityHashMap is a Map implementation that compares keys using == instead of equals().
It is useful when you need to track object identity, not object value.
Perfect for:
- serialization frameworks
- caching by reference
- tracking visited objects
- graphs / tree processing
