Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么此集合包含检查失败?_Java_Equals_Hashset_Effective Java - Fatal编程技术网

Java 为什么此集合包含检查失败?

Java 为什么此集合包含检查失败?,java,equals,hashset,effective-java,Java,Equals,Hashset,Effective Java,我正在阅读有效的Java第三版,我正在阅读第10项:重写时遵循等于契约 这里有一个我试图在我的机器上模拟的例子。下面是相同的代码 public class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Objec

我正在阅读有效的Java第三版,我正在阅读第10项:重写时遵循等于契约

这里有一个我试图在我的机器上模拟的例子。下面是相同的代码

public class Point {
    private int x;
    private int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Point))
            return false;

        Point p = (Point)obj;
        return (x == p.x) && (y ==p.y);
    }

    // Use this for demonstration with AtomicPoint class.
    /*@Override
    public boolean equals(Object obj) {
        if(obj == null  || (obj.getClass() != getClass())) {
            return false;
        }

        Point p = (Point)obj;

        return p.x == x && p.y == y;
    }*/
}

public class AtomicPoint extends Point{
    private static final AtomicInteger counter = new AtomicInteger();

    public AtomicPoint(int x, int y) {
        super(x, y);
        counter.incrementAndGet();
    }

    public static int getCounter() {
        return counter.get();
    }

    private static Set<Point> sampleSet = new HashSet<>();

    public static void main(String[] args) {
        sampleSet.add(new Point(1,2));
        sampleSet.add(new Point(1,3));
        sampleSet.add(new Point(1,4));

        AtomicPoint ap = new AtomicPoint(1,3);

        // This should return true but its returning false
        System.out.println(sampleSet.contains(ap));
    }
}
公共类点{
私人INTX;
私营企业;
公共点(整数x,整数y){
这个.x=x;
这个。y=y;
}
@凌驾
公共布尔等于(对象obj){
如果(!(obj点实例))
返回false;
点p=(点)obj;
收益率(x==p.x)&(y==p.y);
}
//使用此选项对AtomicPoint类进行演示。
/*@凌驾
公共布尔等于(对象obj){
if(obj==null | |(obj.getClass()!=getClass()){
返回false;
}
点p=(点)obj;
返回p.x==x&&p.y==y;
}*/
}
公共类原子点扩展点{
私有静态最终AtomicInteger计数器=新的AtomicInteger();
公共原子点(整数x,整数y){
super(x,y);
counter.incrementAndGet();
}
公共静态int getCounter(){
返回计数器get();
}
私有静态集sampleSet=newhashset();
公共静态void main(字符串[]args){
添加样本集(新点(1,2));
添加样本集(新点(1,3));
添加样本集(新点(1,4));
原子点ap=新原子点(1,3);
//这应该返回true,但返回false
System.out.println(sampleSet.contains(ap));
}
}

正如您从AtomicPoint类中的注释中所看到的,对于包含检查,我得到了false,而Joshua Bloch声明这应该返回true。有人能帮我吗?

要使用
HashSet
HashMap
您需要重写super类中的
hashCode()
方法。您应该在编辑器中自动生成
hashCode()
equals()
方法(我建议您在每个类中始终使用这些方法)。如果你想使用
TreeSet
TreeMap
你需要实现
Comparable
Comparator
接口,并重写它们的
compare()
compareTo()
方法来使用它。

你缺少第11项:“重写等于时总是重写hashCode”-仅覆盖等于是不够的!谢谢,是的,我在书中还没写那么多。并且认为书中的例子依赖于给定项目中解释的概念。