Last Updated: 28 May, 2022
IdentityHashMap is a child class of AbstractMap and implements the Map Interface as well as Serializable and Cloneable Interfaces. It was introduced in the JDK 1.4 version and is available in the java.util package.
IdentityHashMap is used to store the data in the form of key-value pairs where only one null key and multiple null values are allowed.
As we can see from the complete hierarchy above, IdentityHashMap extends the AbstractMap class and implements the Map interface as well as Serializable and Cloneable interfaces.
Here, K is the key Object type and V is the value Object type.
Constructor | Description |
---|---|
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 keys-value mappings in the specified map. |
Java IdentityHashMap class has the following methods.
Methods and Descriptions |
---|
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 a 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. |
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<K> 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. |
int size() Returns the number of key-value mappings in this identity hash map. |
Collection<V> values() Returns a Collection view of the values contained in this map. |
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/IdentityHashMap.html
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
How to synchronise the IdentityHashMap class in Java?
Ans.: IdentityHashMap implementation is not synchronized. If multiple threads try to access an IdentityHashMap 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(...));