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

  • It uses reference equality rather than using the equals() method. It uses the == operator.
  • It is must to synchronize IdentityHashMap externally as it is not synchronized previously.
  • The Iterators throw ConcurrentModificationException in while attempting to modify during the iteration.
  • It provides constant time performance for the basic operations like get and put, assuming that the System.identityHashCode(Object) disperse the elements correctly in the bucket. IdentityHashMap doesn't use hashCode() method instead it uses System.identityHashCode() method. This is a significant difference because now you can use mutable objects as key in Map whose hash code is likely to change when the mapping is stored inside IdentityHashMap.

Syntax:

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:

OR

1. IdentityHashMap():

It creates a new and empty identity hash map with the default expected maximum size that is 21.

Syntax:

2. IdentityHashMap(int ExpectedMaxSize):

It creates a new and empty identity hash map with the given specified expected maximum size.

Syntax:

3. IdentityHashMap(Map m):

It creates a new identity hash map with the key-value pairs given in the specified map.

Syntax:

Methods of the IdentityHashMap Class

Along with the methods inherited from its parent classes, IdentityHashMap defines following methods:

Sr. no.MethodDescription
1.void clear()It clears the map i.e. remove all the mappings.
2.Set entrySet()It returns a set view of the key-value mappings in the map.
3.Set keySet()It returns an identity based set view of keys contained in the hash map.
4.Object put (Object key, Object value)It associate the given value with the given key in the identity hash map.
5.void putAll (Map t)It copies all the mappings from specified map (in the argument) to the current map. These mappings will replace any mappings the map had for any keys currently specified in the map.
6.Object remove (Object key)It removes all the mappings for specified key from the map (if present).
7.int size()It returns the number of key value mappings in the identity hash map.
8.Object clone()It returns a shallow copy of given identity hash map. However, the keys and values are not cloned.
9.boolean containsKey (Object key)It checks whether the specified object is a key in given identity hash map.
10.boolean containsValue (Object value)It checks whether the specified object reference is a value in given identity hash map.
11.boolean equals (Object o)It compares the specified object with the map to check equality.
12.Object get (Object key)It returns the value to which given key is mapped in the identity hash map. And returns null if map contains no mapping for the key.
13.int hashCode()It returns the hash code value of the map.
14.boolean isEmpty()It returns true if the specified identity hash map contains no mappings.
15.Collection values()It returns a collection view of the values placed in the map.

Let's use the methods of the IdentityHashMap class.

IdentityHashMapDemo.java

Output:

IdentityHashMap Class in Java

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.

Example:

Let's consider following example where we implement put() and putAll() methods in a Java program.

IdentityHashMapExample1.java

Output:

IdentityHashMap Class in Java

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.

IdentityHashMapExample2.java

Output:

IdentityHashMap Class in Java

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.

IdentityHashMapExample3.java

Output:

IdentityHashMap Class in Java

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 Entryto resolve two separate types in a compatible format. Then we will use the next() method to print the elements of IdentityHashMap.

IdentityHashMapExample4.java

Output:

IdentityHashMap Class in Java

Difference Between IdentityHashMap and HashMap

  • The IdentityHashMap uses equality operator (==) to compare the key and value while the HashMap uses the equals() method to compare key and value inside the Map.
  • As the IdentityHashMap doesn't use equals() method it is faster than the HashMap.
  • IdentityHashMap doesn't require keys to be immutable as it is not dependant on equals().

The following example explains the difference between IdentityHashMap and HashMap classes.

IdentityHashMapVSHashMap.java

Output:

IdentityHashMap Class in Java

Synchronized IdentityHashMap

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.

Syntax:

where, K is type of keys in the map and V is type of values mapped with it.


Next TopicJava BF
 
 

Latest Courses