📜  Java中的 EnumMap equals() 方法及示例

📅  最后修改于: 2022-05-13 01:55:38.796000             🧑  作者: Mango

Java中的 EnumMap equals() 方法及示例

Java中的Java .util.EnumMap.equals( obj ) 用于将传递的对象与此 EnumMap进行比较是否相等。必须记住,传递的对象必须是与 EnumMap 相同类型的映射。
句法:

boolean equals(Object obj)

参数:该方法接受一个Object类型的参数obj ,并引用要与该地图进行比较的地图。
返回值:如果指定的对象等于地图,则该方法返回true,否则返回false。
下面的程序说明了 equals() 方法的工作:
方案一:

Java
// Java program to demonstrate 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 mp1 = new EnumMap(gfg.class);
 
        EnumMap mp2 = new EnumMap(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);
    }
}


Java
// Java program to demonstrate 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 mp1 = new EnumMap(gdp.class);
 
        EnumMap mp2 = new EnumMap(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);
    }
}


输出:
Map1 equal to Map2: true
Map1 equal to Map2: false

方案二:

Java

// Java program to demonstrate 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 mp1 = new EnumMap(gdp.class);
 
        EnumMap mp2 = new EnumMap(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);
    }
}
输出:
Map1 equal to Map2: true
Map1 equal to Map2: false