• Tags
  •         
  • www.breakyizhan.com
  •    

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

    句法:

    boolean equals(Object obj)

    参数:该方法采用Object类型的一个参数obj,并引用要与此映射进行比较的映射。

    返回值:如果指定的对象等于map,则该方法返回true,否则返回false。

    下面的程序说明了Java.util.EnumMap.equals()方法的工作原理

    程序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);
        }
    }
    

    输出:

    Map1 equal to Map2: true
    Map1 equal to Map2: false

    程序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);
        }
    }
    

    输出:

    Map1 equal to Map2: true
    Map1 equal to Map2: false

     
    转载请保留页面地址:https://www.breakyizhan.com/java/5448.html