forked from jhyscode/Effetive-Java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigram.java
More file actions
48 lines (41 loc) · 1.15 KB
/
Bigram.java
File metadata and controls
48 lines (41 loc) · 1.15 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
package com.github.chapter5;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/**
* EffectiveJava(36) -- 坚持使用Override注解
* @author jhys
* @date 2018/8/1
*/
public class Bigram {
private final char first;
private final char second;
public Bigram(char first, char second) {
this.first = first;
this.second = second;
}
public boolean equals(Bigram b) {
return b.first == first && b.second == second;
}
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (o == null || getClass() != o.getClass()) return false;
// Bigram bigram = (Bigram) o;
// return first == bigram.first &&
// second == bigram.second;
// }
@Override
public int hashCode() {
return 31 * first + second;
}
public static void main(String[] args) {
Set<Bigram> set = new HashSet<>();
for (int i = 0; i < 10; i++) {
for(char c = 'a';c <= 'z';c++) {
set.add(new Bigram(c,c));
}
}
System.out.println("setSize = " + set.size());
}
}