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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
| static class A {
String name;
int age;
public A(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj) {
return new EqualsBuilder().append(getClass(), obj.getClass()).append(name, ((A) obj).name).isEquals();
}
}
@Test
public void testA() {
Set<A> set = new HashSet<>();
set.add(new A("abc", 12));
set.add(new A("abc", 14));
System.out.println(set.size());
System.out.println(set.contains(new A("abc", 0)));
}
static class B extends A {
public B(String name, int age) {
super(name, age);
}
@Override
public int hashCode() {
return this.name.hashCode();
}
}
@Test
public void testB() {
/* Set<A> */Set<B> set = new HashSet<>();
set.add(new B("abc", 12));
set.add(new B("abc", 14));
System.out.println(set.size());
System.out.println(set.contains(new B("abc", 0)));
}
static class AA extends A {
public AA(String name, int age) {
super(name, age);
}
@Override
public boolean equals(Object obj) {
return new EqualsBuilder().append(getClass(), obj.getClass()).append(name, ((A) obj).name)
.append(age, ((A) obj).age).isEquals();
}
@Override
public int hashCode() {
return this.name.hashCode();
}
}
@Test
public void testAA() {
// 实现Map<String, Set<String>>的效果
Set<AA> set = new HashSet<>();
set.add(new AA("abc", 12));
set.add(new AA("abc", 14));
System.out.println(set.size());
System.out.println(set.contains(new AA("abc", 0)));
}
|