java.util.EnumMap.equals() 方法用于比较指定对象与此映射是否相等。如果给定对象也是映射并且两个映射表示相同的映射,则该方法返回 true。
语法
public boolean equals(Object obj)
参数
obj | 指定要与此映射比较是否相等的对象。 |
返回值
如果指定对象等于此映射,则返回 true,否则返回 false。
异常
无。示例:
在下面的示例中,java.util.EnumMap.equals() 方法用于比较 EnumMap 是否相等。
import java.util.*;
public class MyClass {
//创建一个枚举
public enum weekday{
RED, BLUE, GREEN
}
public static void main(String[] args) {
//创建 EnumMap
EnumMap<weekday,Integer> Map1 =
new EnumMap<weekday,Integer>(weekday.class);
EnumMap<weekday,Integer> Map2 =
new EnumMap<weekday,Integer>(weekday.class);
EnumMap<weekday,Integer> Map3 =
new EnumMap<weekday,Integer>(weekday.class);
//关联Map1中的值
Map1.put(weekday.RED, 1);
Map1.put(weekday.BLUE, 2);
Map1.put(weekday.GREEN, 3);
//关联Map2中的值
Map2.put(weekday.RED, 1);
Map2.put(weekday.BLUE, 2);
Map2.put(weekday.GREEN, 3);
//关联Map3中的值
Map3.put(weekday.RED, 10);
Map3.put(weekday.BLUE, 20);
Map3.put(weekday.GREEN, 30);
//检查Map1和Map2是否相等
System.out.println("Map1 and Map2 are equal? - " + Map1.equals(Map2));
//检查Map1和Map2是否相等
System.out.println("Map1 and Map3 are equal? - " + Map1.equals(Map3));
}
}
输出上述代码将是:
Map1 and Map2 are equal? - true
Map1 and Map3 are equal? - false