Categories: Java

IdentityHashMap class in Java

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:
ConstructorDescription
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.
Example:
// 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>();
  new_Id.putAll(id);

  // 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: " +
                                    returned_value);
                                     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
                = itr.next();
            System.out.println("Key = " + entry.getKey()
                               + ", Value = "
                               + entry.getValue());
        }
 }
}
Output:
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.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

View Comments

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago