Java??????У?java.lang.Object
???????????? ???????[ 2014/5/29 13:22:02 ] ????????Java java.lang.Object
????ObjectTest?д????true?????User????????д??equals()?????????????????????????????С?????????????????????ж????????????????????????????ж?uid?????????ɡ?
??????????дequals??????????????????????????????????????Java?е????????дequals()??????????дhasCode()??????
????6.public native int hashCode();
????hashCode()????????????????????????????????????
????hashCode()?????????????
????1).??Java??ó???????????????????????ε???hashCode()?????????????????????????????????????equals??????????????δ????????Java??ó?????????е??????????У????????hashCode()?????????????????£?
????2).????????????????????????equals()????????????????????????hashCode()??????????????????
????3).????????????????hasCode()??????????????????????????????
?????????????????????? ??????????? <=> equals()??? => hashCode()??????????дequlas()??????????дhashCode()????????????????????????????????????hasCode()????? => equals????????? <=> ????????????
???????????????????????????????????????????????????????????????equals???????????ū?????hashCode()?????????????????ū??????鷳??
????????????????????hashCode()????????????????????????????????????
????????????У???Set????????????????????????ж????м??????????????????????????????????hashCode()???????????Set??????α????????????equals()?????ж????????????????????????臨????o(n)???????????hasCode??????????????????????????????????????????????????λ???????ж??λ??????????ж????ɡ??????Set?????????Map?????????
??????????????????????????????????hashCode()???????????????????????????????????????????????????hashCode()???????????????????????????????????????????hashCode()?????????????????
???????????????????У???д??equals()???????????дhashCode()??????
1 package com.corn.objectsummary;
2
3 public class User {
4
5 private int uid;
6 private String name;
7 private int age;
8
9 public int getUid() {
10 return uid;
11 }
12
13 public void setUid(int uid) {
14 this.uid = uid;
15 }
16
17 protected String getName() {
18 return name;
19 }
20
21 public void setName(String name) {
22 this.name = name;
23 }
24
25 public int getAge() {
26 return age;
27 }
28
29 public void setAge(int age) {
30 this.age = age;
31 }
32
33 @Override
34 public boolean equals(Object obj) {
35 if (obj == null || !(obj instanceof User)) {
36 return false;
37 }
38 if (((User) obj).getUid() == this.getUid()) {
39 return true;
40 }
41 return false;
42 }
43
44 @Override
45 public int hashCode() {
46 int result = 17;
47 result = 31 * result + this.getUid();
48 return result;
49 }
50 }
|
??????
???·???
??????????????????
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