IdentityHashMap Class in Java
The IdentityHashMap class is similar to the HashMap class. It implements the AbstractMap class. However, it uses reference equality rather than object equality while comparing the key (or values).
It is not the general purpose implementation of Map. While this class implements the Map interface, it intentionally violates Map's general contract that mandates the use of the equals() method when comparing objects.
It is designed especially for the rare cases where the reference-equality semantics are needed. It is used when the user needs the objects to be compared using the reference.
We need to import java.util package to use IdentityHashMap class.
Features of IdentityHashMap
where, K is the key of Object type and V is the value of Object type.
Constructors of IdentityHashMap
There are two ways to create instance of IdentityHashMap:
It creates a new and empty identity hash map with the default expected maximum size that is 21.
2. IdentityHashMap(int ExpectedMaxSize):
It creates a new and empty identity hash map with the given specified expected maximum size.
3. IdentityHashMap(Map m):
It creates a new identity hash map with the key-value pairs given in the specified map.
Methods of the IdentityHashMap Class
Along with the methods inherited from its parent classes, IdentityHashMap defines following methods:
Let's use the methods of the IdentityHashMap class.
Basic Operations on IdentityHashMap
1. Adding the elements
To add elements into the IdentityHashMap, we have use the put() and putAll() methods. The put() method enters the specified key and value mapped together in the map. When the current key is passed, previous value is replaced by new value. The putAll() method copies all the key value mappings from one map to another.
Let's consider following example where we implement put() and putAll() methods in a Java program.
2. Removing the elements
To delete the mappings (key-value pairs) from the map, we use the remove() method.
Let's consider following example where we use remove() method to delete a mapping in Java program.
3. Accessing the elements
To access the elements of an IdentityHashMap we use the get()method.
Let's consider following example where we use get() method to access the mappings from the map.
4. Traversing through IdentityHashMap
To traverse over the all the structures of Collection Framework, we use Iterator interface. As the Iterators work with only single type of data, we use Entry
Difference Between IdentityHashMap and HashMap
The following example explains the difference between IdentityHashMap and HashMap classes.
When more than one threads access the identity hash map concurrently, and at least one of the threads structurally modifies the map, it is necessary to synchronize that map externally. (Structural modification of map is to add or delete one or more key value mappings. If we just change the value associated with a key that an instance contains already is not structural modification.)
It can be achieved by synchronizing on any object that encapsulate the map. If such object doesn't exist, map should be wrapped with the help of Collections.synchronizedMap() method. The correct time to do this is at the time of creation, in order to prevent unsynchronized access to map.
where, K is type of keys in the map and V is type of values mapped with it.