-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapHashing.java
More file actions
68 lines (56 loc) · 1.73 KB
/
MapHashing.java
File metadata and controls
68 lines (56 loc) · 1.73 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
60
61
62
63
64
65
66
67
68
import java.lang.reflect.*;
import java.util.*;
class Groundhog {
protected int number;
public Groundhog(int n) {
number = n;
}
public String toString() {
return "Groundhog #" + number;
}
}
class Groundhog2 extends Groundhog{
public Groundhog2(int n) {
super(n);
}
@Override
public int hashCode() {
return number;
}
@Override
public boolean equals(Object obj) {
return obj instanceof Groundhog2 && (number == ((Groundhog2)obj).number);
}
}
class Prediction {
private static Random rand = new Random(47);
private boolean shadow = rand.nextDouble() > 0.5;
public String toString() {
if (shadow)
return "Six more weeks of Winter!";
else
return "Early Spring!";
}
} /// :~
// : containers/SpringDetector.java
// What will the weather be?
class SpringDetector {
// Uses a Groundhog or class derived from Groundhog:
public static <T extends Groundhog> void detectSpring(Class<T> type) throws Exception {
Constructor<T> ghog = type.getConstructor(int.class);
Map<Groundhog, Prediction> map = new HashMap<Groundhog, Prediction>();
for (int i = 0; i < 10; i++)
map.put(ghog.newInstance(i), new Prediction());
System.out.println("map = " + map);
Groundhog gh = ghog.newInstance(3);
System.out.println("Looking up prediction for " + gh);
if (map.containsKey(gh))
System.out.println(map.get(gh));
else
System.out.println("Key not found: " + gh);
}
public static void main(String[] args) throws Exception {
//detectSpring(Groundhog.class);
detectSpring(Groundhog2.class);
}
}