?????Java?б???equals?????????????壨???
???????????? ???????[ 2012/9/17 10:13:11 ] ????????
????????2????????equals????????????hashCode?????
??????????????????????Point?????p1??p2a?????????????????????true??????????????????????????HashSet.contains()?????в???????п?????????false??????
Point p1 = new Point(1?? 2);
HashSet System.out.println(coll.contains(p2)); // ??? false (?п???) |
???????????????????????false??????????з???ture????????????????????true???????????????????????????????????????????????в????????????????????????????Point??????equals????????hashCode??
??????????????????????????HashSet??????ζ???????е??????????????????????”???? hash buckets”?С?contains?????????????????????в???????????е?????????????????????б????????????????Point???汾???????equals????????????????????????hashCode??????hashCode?????Object???????汾????????????????????????任??????p1??p2??????????????????????????????????????????????????????????????????м???????????????????в????????С?contains???????????p2?????????????е????????????????????£?p1????????????????У?????p2????????p1??????????p2??p2???????????????????У???????????£?contains?????true???
?????????Point????????????????????Υ???????Object???????hashCode?????塣
??????????????????equals(Object)???????????????????????????????hashCode??????ò???????????
????????????Java?У?hashCode??equals???????????????????????????hashCode???????????equals?????????????????????Point????????????????hashCode???????????????????塣
public class Point {
private final int x;
public Point(int x?? int y) {
public int getX() {
public int getY() {
@Override public boolean equals(Object other) {
@Override public int hashCode() { } |
?????????hashCode????????????x????????41??????????41????????????y???????????????????????????????????????С??????????????????(?????????????????????)??
????????hashCode?????????????????????Point?????????????????????????????????????????????????
????δ?????......
??????
???·???
??????????????????
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