Clean • Professional
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:
An IdentityHashMap is a hash table–based Map implementation that:
==) for comparing keyshashCode()import java.util.IdentityHashMap;

1. Uses Reference Equality (==)
.equals() to compare keys.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:
5. Not for Daily Use
Not recommended for regular business logic because behavior can be confusing.
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);
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.) |
| 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 |
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.
Use it when identity-based comparison is required instead of value comparison. Examples:
IdentityHashMap<K, V> map = new IdentityHashMap<>();
IdentityHashMap<K, V> map = new IdentityHashMap<>(50); // with capacity
IdentityHashMap<K, V> map = new IdentityHashMap<>(anotherMap);
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).
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.
| Operation | Complexity |
|---|---|
| put() | O(1) |
| get() | O(1) |
| remove() | O(1) |
| iteration | O(n) |
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: