forked from jbloch/effective-java-3e-source-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigram2.java
More file actions
34 lines (28 loc) · 859 Bytes
/
Copy pathBigram2.java
File metadata and controls
34 lines (28 loc) · 859 Bytes
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
package effectivejava.chapter6.item40;
import java.util.HashSet;
import java.util.Set;
// Fixed Bigram class (Page 189)
public class Bigram2 {
private final char first;
private final char second;
public Bigram2(char first, char second) {
this.first = first;
this.second = second;
}
@Override public boolean equals(Object o) {
if (!(o instanceof Bigram2))
return false;
Bigram2 b = (Bigram2) o;
return b.first == first && b.second == second;
}
public int hashCode() {
return 31 * first + second;
}
public static void main(String[] args) {
Set<Bigram2> s = new HashSet<>();
for (int i = 0; i < 10; i++)
for (char ch = 'a'; ch <= 'z'; ch++)
s.add(new Bigram2(ch, ch));
System.out.println(s.size());
}
}