java中的Hashtable怎么用,请详细举例子说明,拜托了 谢谢

java中的Hashtable怎么用,请详细举例子说明,拜托了 谢谢

就芦拆携是哈希表陪伏,下面这个示例创建了一个数字的哈希表。它将数字的名称用作键:   Hashtable numbers   = new Hashtable();  
 numbers.put("one", 1);  
 numbers.put("two", 2);  
 御皮numbers.put("three", 3);   
要获取一个数字,可以使用以下代码:
  Integer n = numbers.get("two");  
 if (n != null) {  
 System.out.println("two = " + n);  
 }
Hashtable hm = new Hashtable();
hm.put("003","wangwu");
hm.put("001","zhangsan"枣或判);
hm.put("凳改002","lisi");
Set keySet = hm.keySet();
Iterator it = keySet.iterator();
while(it.hasNext())
{
String num = it.next();
String value = hm.get(num);
System.out.println(num+":"+value);
}
注:Hashtable是线程安全的,不允许存放null键null值。现在已经被HashMap所代团闷替。
table就是表嘛
Hashtable是按照Hash算法指握进行分配位置的表。
Hash算法还是要看看的姿逗模,毕竟Hash在Java中无处迹缓不在呀。每个类都从Object继承了方法hashCode(),在这就应该去看看Hash是啥东东,呵呵。
← 上一篇: 下一篇: →