?????Java?б???equals?????????????壨?£?
???????????? ???????[ 2012/9/18 10:13:57 ] ????????
????????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
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11