Java IdentityHashMap equals() Method



Description

The Java IdentityHashMap equals(Object o) method is used to compare the specified object with this map for equality.

Declaration

Following is the declaration for java.util.IdentityHashMap.equals() method.

public boolean equals(Object o)

Parameters

o − This is the object to be compared for equality with this map.

Return Value

The method call returns 'true' if the specified object is equal to this map.

Exception

NA

Checking Equality of IdentityHashMap Objects of Integer, Integer Pair Example

The following example shows the usage of Java IdentityHashMap equals() method to check if map objects are same or not. We've created two Map objects of Integer,Integer pairs. Then few entries are added, maps are compared using equals() method.

package com.tutorialspoint;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
   public static void main(String args[]) {
      
      // create identity map
      IdentityHashMap<Integer,Integer> newmap = new IdentityHashMap<>();
      IdentityHashMap<Integer,Integer> newmap2 = new IdentityHashMap<>();

      // populate identity map
      newmap.put(1, 1);
      newmap.put(2, 2);
      newmap.put(3, 3); 
	  
      // populate identity map
      newmap2.put(1, 4);
      newmap2.put(2, 5);
      newmap2.put(3, 6); 

      System.out.println("Maps are equal: " + newmap.equals(newmap2));
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Maps are equal: false

Checking Equality of IdentityHashMap Objects of Integer, String Pair Example

The following example shows the usage of Java IdentityHashMap equals() method to check if map objects are same or not. We've created two Map objects of Integer,String pairs. Then few entries are added, maps are compared using equals() method.

package com.tutorialspoint;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
   public static void main(String args[]) {
      
      // create map
      IdentityHashMap<Integer,String> newmap = new IdentityHashMap<>();
      IdentityHashMap<Integer,String> newmap2 = new IdentityHashMap<>();

      // populate map
      newmap.put(1, "tutorials");
      newmap.put(2, "point");
      newmap.put(3, "is best"); 
	  
      // populate map
      newmap2.put(1, "A");
      newmap2.put(2, "B");
      newmap2.put(3, "C"); 

      System.out.println("Maps are equal: " + newmap.equals(newmap2));
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Maps are equal: false

Checking Equality of IdentityHashMap Objects of Integer, Object Pair Example

The following example shows the usage of Java IdentityHashMap equals() method to check if map objects are same or not. We've created two Map objects of Integer,Student pairs. Then few entries are added, maps are compared using equals() method.

package com.tutorialspoint;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
   public static void main(String args[]) {
      
      // create map
      IdentityHashMap<Integer,Student> newmap = new IdentityHashMap<>();
      IdentityHashMap<Integer,Student> newmap2 = new IdentityHashMap<>();

      // populate map
      newmap.put(1, new Student(1, "Julie"));
      newmap.put(2, new Student(2, "Robert"));
      newmap.put(3, new Student(3, "Adam"));

      // populate map
      newmap2.put(1, new Student(1, "Julie"));
      newmap2.put(2, new Student(2, "Robert"));
      newmap2.put(3, new Student(4, "Jene"));
	  
      System.out.println("Maps are equal: " + newmap.equals(newmap2));
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result.

Maps are equal: false
java_util_identityhashmap.htm
Advertisements