package map;
public class Groundhog {
protected int number;
public Groundhog(int number) {
super();
this.number = number;
}
/**必须覆盖object中的equals和hashCode方法,否则无法用散列处理key,注意别写成了hashcode*/
/* public boolean equals(Object o) {
return o instanceof Groundhog && (this.number == ((Groundhog) o).number);
}
public int hashCode() {
return this.number;
}*/
public String toString() {
return "Groundhog #" + number;
}
}
package map;
import java.util.Random;
public 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!";
}
}
}
package map;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.HashMap;
public class SpringDetector {
public static void detectSpring(Class type) throws Exception {
Constructor ghog = type.getConstructor(int.class);
Map map = new HashMap();
for (int i = 0; i < 10; i++) {
map.put(ghog.newInstance(i), new Prediction());
}
System.out.println("map=" + map);
Groundhog ghog3 = new Groundhog(3);
System.out.println("Looking up Prediction for " + ghog3);
if (map.containsKey(ghog3)) {
System.out.println(map.get(ghog3));
} else {
System.out.println("key not found!");
}
}
public static void main(String[] args) throws Exception {
detectSpring(Groundhog.class);
}
}
Goundhog自动继承基类Object,默认使用对象的地址计算散列码,由于反射生成第一个Goundhog(3)实例与直接生成的Goundhog(3)地址显然不相等, 因此在map查询不到这个key。
总结:要使用自己的类作为key,必须同时重载hashCode()和equals()。