forked from jhyscode/Effetive-Java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTest.java
More file actions
59 lines (52 loc) · 1.27 KB
/
HashTest.java
File metadata and controls
59 lines (52 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.github.chapter2;
import java.util.HashSet;
import java.util.Set;
public class HashTest {
private int i;
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public int hashCode() {
return i % 10;
}
/**
* 两个对象的hashCode相同,并不一定表示两个对象就相同
* @param obj
* @return
*/
@Override
public boolean equals(Object obj) {
//对象为空
if(obj == null) {
return false;
}
//对象等于本身
if(obj == this) {
return true;
}
//对象不属于HashTest类,直接返回false
if(!(obj instanceof HashTest)) {
return false;
}
HashTest other = (HashTest) obj;
if(other.getI() == this.getI()) {
return true;
}
return false;
}
public static void main(String[] args) {
HashTest a = new HashTest();
HashTest b = new HashTest();
a.setI(1);
b.setI(1);
Set<HashTest> set = new HashSet<>();
set.add(a);
set.add(b);
System.out.println(a.hashCode() == b.hashCode());
System.out.println(a.equals(b));
System.out.println(set);
}
}