我知道当有人在子类中添加新方面时,不可能扩展覆盖方法 equals()
的类并使其“保留”。类 Point
及其子类的常见示例演示了它:
public class Point {
Double d1;
Double d2;
public Point(double d1, double d2){
this.d1 = d1;
this.d2 = d2;
}
}
public class ColorPoint extends Point {
String color;
public ColorPoint(double d1, double d2, String s) {
super(d1, d2);
color = s;
}
}
如果我们让 Eclipse 创建方法 equals()
和 hashCode()
,它还会考虑 ColorPoint< 的颜色属性
。因此 equals()
方法被证明是不对称的。代码:
Point p1 = new Point(2,2);
ColorPoint cp1 = new ColorPoint(2, 2, "blue");
System.out.println(p1.equals(cp1));
System.out.println(cp1.equals(p1));
打印:
是的 假的
同理可以证明该方法是不可传递的。但是,当我将对象作为 HasMap
中的键传递时,它会将它们识别为不同的,而不管我传递它们的顺序如何。代码:
Point p1 = new Point(2,2);
Point p2 = new Point(3.1,3.1);
ColorPoint cp1 = new ColorPoint(2, 2, "blue");
ColorPoint cp2 = new ColorPoint(3.1,3.1, "red");
Map<Point, Integer> map = new HashMap<>();
map.put(cp2, 4); map.put(cp1, 3);
map.put(p1, 1); map.put(p2, 2);
System.out.println(map.size());
始终打印 4,即使我以另一个顺序传递对象也是如此。这是预期的吗?那么,Map
使用哪种方法来比较键?
最佳答案
这可能是因为eclipse生成的hashcode()考虑到了ColourPoint的color字段,所以points和colorpoints hash到不同的buckets,永远不会和equals()比较。
请注意,这意味着 hashcode() 的约定已被破坏 - a.equals(b) == true 的两个对象正在生成不同的哈希码。基本上,不要这样做!
Scala 语言对此有一个有趣的看法,它使用 canEqual 方法来确定两个对象是否永远相等。看看here .
关于java - Map键比较中使用的equals方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16651445/