向Hashtable对象中存储数据,使用的是Hashtable.put(Object key, Object value)方法,从Hashtable中检索数据,使用Hashtable.get(Object key)方法。值和关键字都可以是任何类型的非空对象。
下面代码生成一个存储数字的Hashtable,用英文数字作为关键字。
Hashtable numbers = new Hashtabel();
numbers.put("one", new Integer(1));
numbers.put("two", new Integer(2));
numbers.put("three", new Integer(3));
要想检索其中"two"关键字对应的数据,看下面的代码就能明白。
Integer n = (Integer)numbers.get("two");
if(n != null) { System.out,println("two = " + n);}
要想成功地从Hashtable中检索数据,用作关键字的对象必须正确覆盖Object.hashCode方法和Object.equals方法。覆盖Object.equals方法道理不难想象,检索数据时要比较用来检索的关键字是否与存储在Hashtable中的某个关键字相等,如果两个关键字对象不能正确判断是否相等,检索是不可能正确的。Object.hashCode方法回返一个叫散列码的值,这个值是由对象的地址以某种方式转换来的。内容相同的两个对象,既然是两个对象地址就不可能一样,所以Object.hashCode返回的值也不一样。要想两个内容相同的对象的hashCode方法返回一样的散列码,子类必须覆盖Object.hashCode方法,用于关键字的类,如果它的两个对象用equals方法比较相等的,那么这两个对象的hashcode方法返回值也要一样,所以我们也要覆盖hashCode方法。
这里可以提一下String类,因为String类已按照关键字类的要求覆盖了这两个方法,如果两个String对象内容不相等,它们的hashCode的返回值也不会相等。如果两个String对象的内容相等,它们的hashCode的返回值也相等
注意:StringBuffer类没有按照关键字类的要求覆盖hashCode方法,即使两个StringBuffer类对象的内容相等,但这两个对象的hashCode方法的返回值却不相等。
class MyKey{
private String name;
private int age;
public MyKey(String name, int age){
this.name = name;
this.age = age;
}
public String toString(){
return new String(name + ", " + age);
}
public boolean equals(Object obj){
if(name.equals(obj.name) && age == obj.age){
return true;
}else{
return false;
}
}
public int hashCode(){
return name.hashCode() + age;
}
}
import java.util.*;
public class TestHashtable{
public static void main(String[] args){
Hashtable numbers = new Hashtable();
numbers.put(new MyKey("A1", 18), new Integer(1));
numbers.put(new MyKey("A2", 13), new Integer(2));
numbers.put(new MyKey("A3", 15), new Integer(3));
Enumeration e = numbers.keys();
while(e.hasMoreElements()){
MyKey key = (MyKey)e.nextElement();
System.out.println(key.toString() + "=");
System.out.println(numbers.get(key).toString());
}
}
}