Facebook Page Twitter Page LinkedIn Page

Java.util.IdentityHashMap Class



Java IdentityHashMap Class

Java.util package provides an IdentityHashMap class which implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values). In other words, in an IdentityHashMap, two keys k1 and k2 are considered equal if and only if (k1==k2). (In normal Map implementations (like HashMap) two keys k1 and k2 are considered equal if and only if (k1==null ? k2==null : k1.equals(k2)).)

This class provides all of the optional map operations, and permits null values and the null key. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

Class declaration

The declaration of java.util.IdentityHashMap class is:

public class IdentityHashMap<K,V>
  extends AbstractMap<K,V>
    implements Map<K,V>, Serializable, Cloneable

Here, K and V are the type of key and value respectively maintained by the container.

Class Constructors

S.NConstructors & Description
1. IdentityHashMap()
Constructs a new, empty identity hash map with a default expected maximum size (21).
2. IdentityHashMap(int expectedMaxSize)
Constructs a new, empty map with the specified expected maximum size.
3. IdentityHashMap(Map<? extends K,? extends V> m)
Constructs a new identity hash map containing the keys-value mappings in the specified map.

java.util.IdentityHashMap Methods

The java.util.IdentityHashMap has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. void clear()
Removes all of the mappings from this map.
2. Object clone()
Returns a shallow copy of this identity hash map: the keys and values themselves are not cloned.
3. boolean containsKey(Object key)
Tests whether the specified object reference is a key in this identity hash map.
4. boolean containsValue(Object value)
Tests whether the specified object reference is a value in this identity hash map.
5. Set<Map.Entry<K,V>> entrySet()
Returns a Set view of the mappings contained in this map.
6. boolean equals(Object obj)
Compares the specified object with this map for equality.
7. void forEach(BiConsumer<? super K,? super V> action)
Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
8. 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.
9. int hashCode()
Returns the hash code value for this map.
10. boolean isEmpty()
Returns true if this identity hash map contains no key-value mappings.
11. Set<K> keySet()
Returns an identity-based set view of the keys contained in this map.
12. V put(K key, V value)
Associates the specified value with the specified key in this identity hash map.
13. void putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map.
14. V remove(Object key)
Removes the mapping for this key from this map if present.
15. 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.
16. int size()
Returns the number of key-value mappings in this identity hash map.
17. Collection<V> values()
Returns a Collection view of the values contained in this map.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object
  • java.util.AbstractMap<K,V>