Java中的Java.util.EnumMap.equals(obj)用於比較傳遞的對象與此EnumMap的相等性。必須記住,傳遞的對象必須是與EnumMap相同類型的映射。
用法:
boolean equals(Object obj)
參數:該方法采用Object類型的一個參數obj,表示要與此Map進行比較的Map。
返回值:如果指定對象等於Map,則該方法返回true,否則返回false。
以下示例程序旨在說明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
相關用法
- Java EnumMap clear()用法及代碼示例
- Java Map equals()用法及代碼示例
- Java Set equals()用法及代碼示例
- Java Character.equals()用法及代碼示例
- Java AbstractSet equals()用法及代碼示例
- Java SimpleTimeZone equals()用法及代碼示例
- Java SortedMap equals()用法及代碼示例
- Java Float equals()用法及代碼示例
- Java Instant equals()用法及代碼示例
- Java ChoiceFormat equals()用法及代碼示例
- Java OptionalInt equals()用法及代碼示例
- Java ChronoZonedDateTime equals()用法及代碼示例
- Java Long equals()用法及代碼示例
- Java SortedSet equals()用法及代碼示例
- Java StringCharacterIterator equals()用法及代碼示例
注:本文由純淨天空篩選整理自akash1295大神的英文原創作品 EnumMap equals() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。