Skip to content
Related Articles

Related Articles

EnumMap equals() Method in Java with Examples
  • Last Updated : 11 Jul, 2018
GeeksforGeeks - Summer Carnival Banner

The Java.util.EnumMap.equals(obj) in Java is used to compare the passed object with this EnumMap for the equality. It must be kept in mind that the object passed must be a map of the same type as the EnumMap.

Syntax:

boolean equals(Object obj)

Parameter: The method takes one parameter obj of Object type and refers to the map to be compared with this map.

Return Value: If the specified object is equal to the map, the method returns true else false.

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



Program 1:




// Java program to demonsrate equals() method
import java.util.*;
  
// An enum of gfg ranking worldwide and in India
public enum gfg {
    Global_2018,
    India_2018,
    China_2018
}
;
  
class Enum_map {
    public static void main(String[] args)
    {
  
        EnumMap<gfg, Integer> mp1 = new EnumMap<gfg, Integer>(gfg.class);
  
        EnumMap<gfg, Integer> mp2 = new EnumMap<gfg, Integer>(gfg.class);
  
        // Values are associated in mp1
        mp1.put(gfg.Global_2018, 800);
        mp1.put(gfg.India_2018, 72);
  
        // Values are associated in mp2
        mp2.put(gfg.Global_2018, 800);
        mp2.put(gfg.India_2018, 72);
  
        // Stores the result
        boolean res1 = mp1.equals(mp2);
  
        // Prints the result
        System.out.println("Map1 equal to Map2: " + res1);
  
        mp2.put(gfg.China_2018, 1607);
  
        // Stores the result
        boolean res2 = mp1.equals(mp2);
  
        // Prints the result
        System.out.println("Map1 equal to Map2: " + res2);
    }
}
Output:
Map1 equal to Map2: true
Map1 equal to Map2: false

Program 2:




// Java program to demonsrate equals() method
import java.util.*;
  
// an enum of gdp growth rate
// in recent years of India
public enum gdp {
    Ind_2015,
    Ind_2016,
    Ind_2017,
    Ind_2018,
    Ind_2019
}
;
  
class Enum_map {
    public static void main(String[] args)
    {
  
        EnumMap<gdp, String> mp1 = new EnumMap<gdp, String>(gdp.class);
  
        EnumMap<gdp, String> mp2 = new EnumMap<gdp, String>(gdp.class);
  
        // Values are associated in mp1
        mp1.put(gdp.Ind_2015, "8.4");
        mp1.put(gdp.Ind_2016, "9.2");
        mp1.put(gdp.Ind_2017, "6.1");
        mp1.put(gdp.Ind_2018, "7.7");
  
        // Values are associated in mp2
        mp2.put(gdp.Ind_2015, "8.4");
        mp2.put(gdp.Ind_2016, "9.2");
        mp2.put(gdp.Ind_2017, "6.1");
        mp2.put(gdp.Ind_2018, "7.7");
  
        // Stores the result
        boolean res1 = mp1.equals(mp2);
  
        // Prints the result
        System.out.println("Map1 equal to Map2: " + res1);
  
        mp2.put(gdp.Ind_2019, "7.0");
  
        // Stores the result
        boolean res2 = mp1.equals(mp2);
  
        // Prints the result
        System.out.println("Map1 equal to Map2: " + res2);
    }
}
Output:
Map1 equal to Map2: true
Map1 equal to Map2: 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 :