????????3?????????仯??????equals????

????????????Point???????????С??仯

public class Point {

    private int x;
    private int y;

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

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void setX(int x) { // Problematic
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override public boolean equals(Object other) {
        boolean result = false;
        if (other instanceof Point) {
            Point that = (Point) other;
            result = (this.getX() == that.getX() && this.getY() == that.getY());
        }
        return result;
    }

    @Override public int hashCode() {
        return (41 * (41 + getX()) + getY());
    }
}

??????????x??y??????final??????????set??????????????????????????????x??y?????equals??hashCode??????????????????????????????????仯???????????????????????????????????????????????point???????????????????????????Ч????

Point p = new Point(1?? 2);

HashSet<Point> coll = new HashSet<Point>();
coll.add(p);

System.out.println(coll.contains(p)); // ??? true

???????????????p?е??????????????л??????point???????????????

p.setX(p.getX() + 1);

System.out.println(coll.contains(p)); // (?п???)??? false