Skip to content
Related Articles

Related Articles

AbstractMap equals() Method in Java with Examples
  • Last Updated : 26 Oct, 2018

The AbstractMap.equals() method in Java is used to check for equality between two maps. It verifies whether the elements of one map passed as a parameter is equal to the elements of this map or not.

Syntax:

AbstractMap1.equals(AbstractMap2)

Parameters: The method accepts one parameter ihashmap2 of abstract hash map type and refers to the map whose equality is to be checked with this abstract map.

Return Value: The method returns true if the equality holds for both the object map else it returns false.

Below programs illustrate the working of AbstractMap.equals() method:



Program 1:




// Java code to illustrate the equals() method
import java.util.*;
  
public class Abstract_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty AbstractMap
        AbstractMap<Integer, String>
            abstract_hash1 = new IdentityHashMap<Integer, String>();
  
        AbstractMap<Integer, String>
            abstract_hash2 = new IdentityHashMap<Integer, String>();
  
        // Mapping string values to int keys
        abstract_hash1.put(10, "Geeks");
        abstract_hash1.put(15, "4");
        abstract_hash1.put(20, "Geeks");
        abstract_hash1.put(25, "Welcomes");
        abstract_hash1.put(30, "You");
  
        // Mapping string values to int keys
        abstract_hash2.put(10, "Geeks");
        abstract_hash2.put(15, "4");
        abstract_hash2.put(20, "Geeks");
        abstract_hash2.put(25, "Welcomes");
        abstract_hash2.put(30, "You");
  
        // Displaying the AsbstractMap
        System.out.println("First Map: "
                           + abstract_hash1);
  
        // Displaying the final AbstractMap
        System.out.println("Second Map: "
                           + abstract_hash2);
  
        // Displaying the equality
        System.out.println("Equality: "
                           + abstract_hash1.equals(abstract_hash2));
    }
}
Output:
First Map: {10=Geeks, 30=You, 20=Geeks, 25=Welcomes, 15=4}
Second Map: {10=Geeks, 30=You, 20=Geeks, 25=Welcomes, 15=4}
Equality: true

Program 2:




// Java code to illustrate the equals() method
  
import java.util.*;
  
public class Abstract_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty AbstractMap
        AbstractMap<Integer, String>
            abstract_hash1 = new IdentityHashMap<Integer, String>();
  
        AbstractMap<Integer, String>
            abstract_hash2 = new IdentityHashMap<Integer, String>();
  
        // Mapping string values to int keys
        abstract_hash1.put(10, "Geeks");
        abstract_hash1.put(15, "4");
        abstract_hash1.put(20, "Geeks");
        abstract_hash1.put(25, "Welcomes");
        abstract_hash1.put(30, "You");
  
        // Mapping string values to int keys
        abstract_hash2.put(10, "Geeks");
        abstract_hash2.put(15, "4");
        abstract_hash2.put(20, "Geek");
        abstract_hash2.put(25, "Welcomes");
        abstract_hash2.put(30, "You");
  
        // Displaying the IdentityHashMap
        System.out.println("First Map: "
                           + abstract_hash1);
  
        // Displaying the final IdentityHashMap
        System.out.println("Second Map: "
                           + abstract_hash2);
  
        // Displaying the equality
        System.out.println("Equality: "
                           + abstract_hash1.equals(abstract_hash2));
    }
}
Output:
First Map: {10=Geeks, 30=You, 20=Geeks, 25=Welcomes, 15=4}
Second Map: {10=Geeks, 30=You, 20=Geek, 25=Welcomes, 15=4}
Equality: false

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :