Java中的 AbstractMap.SimpleEntry equals(Object o) 方法及示例
AbstractMap.SimpleEntry
AbstractMap.SimpleEntry
两个条目 e1 和 e2 表示相同的映射,如果
(e1.getKey()==null ?
e2.getKey()==null :
e1.getKey().equals(e2.getKey()))
&&
(e1.getValue()==null ?
e2.getValue()==null :
e1.getValue().equals(e2.getValue()))
句法:
public V equals(Object o)
参数:此方法接受要与此映射条目比较是否相等的对象。
返回值:如果指定对象等于此映射条目,则此方法为 true。
下面的程序说明了 equals(Object o) 方法:
方案一:
// Java program to demonstrate
// AbstractMap.SimpleEntry.equals() method
import java.util.*;
public class GFG {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args)
{
// create two maps
AbstractMap.SimpleEntry map1
= new AbstractMap.SimpleEntry(0, 123);
AbstractMap.SimpleEntry map2
= new AbstractMap.SimpleEntry(0, 123);
// compare both maps
boolean answer = map1.equals(map2);
// print result
System.out.println("Map 1 is equal to Map2 -> "
+ answer);
}
}
输出:
Map 1 is equal to Map2 -> true
方案二:
// Java program to demonstrate
// AbstractMap.SimpleEntry.equals() method
import java.util.*;
public class GFG {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args)
{
// create two maps
AbstractMap.SimpleEntry map1
= new AbstractMap
.SimpleEntry("Captain:", "Dhoni");
AbstractMap.SimpleEntry map2
= new AbstractMap
.SimpleEntry("Captain:", "Kohli");
// compare both maps
boolean answer = map1.equals(map2);
// print result
System.out.println("Map 1 is equal to Map2 -> "
+ answer);
}
}
输出:
Map 1 is equal to Map2 -> false
参考资料: https: Java Java.lang.Object)