IdentityHashMap class in Java

  • September 14, 2022
  • Java
java thread class

The HashMap class is similar to the IdentityHashMap class. The IdentityHashMap implements the Map interface using Hashtable, and when comparing keys, it uses reference-equality rather than object-equality (and values). This class is not intended to be a general-purpose Map implementation. This class is used when the user wants to compare objects by reference. It is part of java.util package.

Constructors of the IdentityHashMap class:
IdentityHashMap()Creates a new, empty identity hash map with a default maximum expected size (21).
IdentityHashMap(int expectedMaxSize)Creates a new, empty map with the specified maximum size.
IdentityHashMap(Map m)Creates a new identity hash map with the key-value mappings from the specified map.
Methods of AbstractList Class:
Method Description
clear()This function removes all mappings from this map.
clone()This function returns a shallow copy of this identity hash map: the keys and values are not cloned.
containsKey(Object key)This function determines whether the specified object reference is a key in this identity hash map.
containsValue(Object value)The specified object reference is checked to see if it is a value in this identity hash map.
entrySet()This method returns a Set view of the mappings in this map.
equals(Object o)For equality, compares the specified object to this map.
get(Object key)Returns the value to which the specified key is mapped, or null if there is no mapping for the key in this map.
hashCode()Returns the hash code value for this map.
isEmpty()If this identity hash map contains no key-value mappings, this function returns true.
keySet()This method returns an identity-based set view of the keys in this map.
put(K key, V value)In this identity hash map, the specified value is associated with the specified key.
putAll(Map<? extends K,? extends V> m)All mappings from the specified map are copied to this map.
remove(Object key)If present removes the mapping for this key from this map.
size()The number of key-value mappings in this identity hash map is returned.
values()This method returns a Collection view of the values in this map.
// Java code to illustrate
// adding elements to IdentityHashMap
import java.util.*;

public class Coderz{

	public static void main(String[] args)
		// Creating an empty IdentityHashMap
		Map<Integer, String> id
			= new IdentityHashMap<Integer, String>();

		// Mapping string values to int keys
		// using put() method
		id.put(10, "A");
		id.put(15, "4");
		id.put(20, "Java");
		id.put(25, "Month");
		id.put(30, "cake");

		// Displaying the IdentityHashMap
		System.out.println("Initial Mappings are: "
						+ id);

		// Inserting existing key along with new value
		// previous value gets returned and stored in
		// returned_value
		String returned_value
			= (String)id.put(20, "All");

		// Verifying the returned value
		System.out.println("Returned value is: "
						+ returned_value);

		// Displaying the new map
		System.out.println("New map is: " + id);

		// Creating a new Identityhash map and copying
		Map<Integer, String> new_Id
			= new IdentityHashMap<Integer, String>();

		// Displaying the final IdentityHashMap
		System.out.println("The new map: "
						+ new_Id);
		 returned_value =(String)id.remove(10);
        // Verifying the returned value
        System.out.println("Returned value is: " +
                                     Iterator<IdentityHashMap.Entry<Integer, String> >
            itr = id.entrySet().iterator();
        // The hasNext() method is used to check if there is
        // a next element The next() method is used to
        // retrieve the next element
        while (itr.hasNext()) {
            IdentityHashMap.Entry<Integer, String> entry
            System.out.println("Key = " + entry.getKey()
                               + ", Value = "
                               + entry.getValue());
Initial Mappings are: {30=cake, 10=A, 15=4, 25=Month, 20=Java}
Returned value is: Java
New map is: {30=cake, 10=A, 15=4, 25=Month, 20=All}
The new map: {30=cake, 10=A, 15=4, 25=Month, 20=All}
Returned value is: A
Key = 30, Value = cake
Key = 15, Value = 4
Key = 25, Value = Month
Key = 20, Value = All

Note: also read about the EnumSet in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

1 comment

Leave a Reply

Your email address will not be published. Required fields are marked *