Java IdentityHashMap Example

IdentityHashMap implements the Map interface and two keys are considered equal when checked as k1==k2 (not by using equals method). This itself violates the Map‘s general contract, which means that IdentityHashMap is clearly not a general-purpose Map implementation. There are only so many cases where this class would be useful.

java-featured-image

IdentityHashMap permits both null values and the null key, alongside all optional map operations. However, this class does not guarantee that the order will remain constant over time.

In terms of time complexity, this class provides constant-time performance for the basic operations (like get and put). 

In addition, it is important to note that this class is not dynamic in the sense that a maximum size has to be given (just like in arrays). In the event of the size of the map being sufficiently exceeded, it will be quite expensive, so this is why it is a good idea to always give a large maximum size. (less expensive, than a simple mistake!)

IdentityHashMap

IdentityHashMap: what implements and extends.

Constructors in IdentityHashMap

  1. IdentityHashMap(): creates a new identity hash map with a default maximum size of 21.
  2. IndetityHashMap(int expectedMaxSize): creates a new empty map with the expected maximum size.
  3. IdentityHashMap(Map<? extends K, ? extends V> m): creates a new identity has map containing the same mappings as the specified map.




Methods in IdentityHashMap

  1. void clear(): removes all mappings from this map.
  2. Object clone(): returns a copy of this identity hash map (important to note that the keys and the values themselves are not cloned).
  3. boolean containsKey(Object key): returns true if the map contains the specified key, false otherwise.
  4. boolean containsValue(Object value): returns true if the map contains the specified value, false otherwise.
  5. boolean equals(Object o): compares the specified object with this map for equality.
  6. V get(Object key): returns the value to which the specified key is mapped, or null if the map contains no mapping to the key.
  7. int hashCode(): returns the hash code value for this map.
  8. boolean isEmpty(): returns true if the map is empty and false otherwise.
  9. V put(K key, V value): associates the specified value to the specified key in the map.
  10. V remove(Object key): removes the mapping to the specified key in the map if present.
  11. int size(): returns the number of key-value mappings in this identity hash map.

For documentation for all the methods, you can visit the Oracle official documentation page.

Example of a program that uses most of the methods above:

import java.util.Map; 
import java.util.HashMap; 
import java.util.IdentityHashMap; 
  
public class IdentityHashMapExample  
{ 
    public static void main(String[] args)  
    { 
        Map identityHashMap = new IdentityHashMap(); 

        identityHashMap.put("key", "value");  
        System.out.println("Size of IdentityHashMap: " + identityHashMap.size());  

        identityHashMap.put("key1", "value1");
        System.out.println("Size of IdentityHashMap: " + identityHashMap.size());  

        System.out.println("Does it contain key 'key1': " + identityHashMap.containsKey("key1"));

        System.out.println("Value of key 'key1': " + identityHashMap.get("key1"));

        System.out.println("Size of map before clear: " + identityHashMap.size());

        identityHashMap.clear();

        System.out.println("Size of map after clear: " + identityHashMap.size());
    } 
}

Output:

Size of IdentityHashMap: 1
Size of IdentityHashMap: 2
Does it contain key 'key1': true
Value of key 'key1': value1
Size of map before clear: 2
Size of map after clear: 0

 

Leave a Reply

avatar