Java Utility Library

Java IdentityHashMap - equals() Method



The java.util.IdentityHashMap.equals() method is used to compare the specified object with this map for equality. The method returns true if the given object is also a map and the two maps represent identical object-reference mappings.

Syntax

public boolean equals(Object obj)

Parameters

obj Specify the object to be compared for equality with this map.

Return Value

Returns true if the specified object is equal to this map.

Exception

NA

Example:

In the example below, the java.util.IdentityHashMap.equals() method is used to compare the specified Object with the given identity hash map.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating identity hash maps
    IdentityHashMap<Integer, String> Map1 = new IdentityHashMap<Integer, String>();
    IdentityHashMap<Integer, String> Map2 = new IdentityHashMap<Integer, String>();
    IdentityHashMap<Integer, String> Map3 = new IdentityHashMap<Integer, String>();
    
    //populating Map1
    Map1.put(101, "John");
    Map1.put(102, "Marry");
    Map1.put(103, "Kim");

    //populating Map2
    Map2.put(101, "John");
    Map2.put(102, "Marry");
    Map2.put(103, "Kim");

    //populating Map3
    Map3.put(1, "JAN");
    Map3.put(2, "FEB");
    Map3.put(3, "MAR");

    //checking Map1 and Map2 for equality
    System.out.print("Are Map1 and Map2 equal? : ");  
    System.out.println(Map1.equals(Map2)); 

    //checking Map1 and Map3 for equality
    System.out.print("Are Map1 and Map3 equal? : ");  
    System.out.println(Map1.equals(Map3));  
  }
}

The output of the above code will be:

Are Map1 and Map2 equal? : true
Are Map1 and Map3 equal? : false

❮ Java.util - IdentityHashMap