(1)HashTable介绍
和HashMap一样,Hashtable 也是一个散列表,它存储的内容是键值对(key-value)映射。
Hashtable 继承于Dictionary,实现了Map、Cloneable、java.io.Serializable接口。
Hashtable 的函数都是同步的,这意味着它是线程安全的。它的key、value都不可以为null。此外,Hashtable中的映射不是有序的。
Hashtable 的实例有两个参数影响其性能:初始容量 和 加载因子。容量 是哈希表中桶 的数量,初始容量 就是哈希表创建时的容量。注意,哈希表的状态为 open:在发生“哈希冲突”的情况下,单个桶会存储多个条目,这些条目必须按顺序搜索。加载因子 是对哈希表在其容量自动增加之前可以达到多满的一个尺度。初始容量和加载因子这两个参数只是对该实现的提示。关于何时以及是否调用 rehash 方法的具体细节则依赖于该实现。
通常,默认加载因子是 0.75, 这是在时间和空间成本上寻求一种折衷。加载因子过高虽然减少了空间开销,但同时也增加了查找某个条目的时间(在大多数 Hashtable 操作中,包括 get 和 put 操作,都反映了这一点)。
(2)Hashtable的构造函数和API
1、构造函数
// 默认构造函数。
public Hashtable()
// 指定“容量大小”的构造函数
public Hashtable(int initialCapacity)
// 指定“容量大小”和“加载因子”的构造函数
public Hashtable(int initialCapacity, float loadFactor)
// 包含“子Map”的构造函数
public Hashtable(Map<? extends K, ? extends V> t)
2、API
synchronized void clear()
synchronized Object clone()
boolean contains(Object value)
synchronized boolean containsKey(Object key)
synchronized boolean containsValue(Object value)
synchronized Enumeration elements()
synchronized Set<Entry<K, V>> entrySet()
synchronized boolean equals(Object object)
synchronized V get(Object key)
synchronized int hashCode()
synchronized boolean isEmpty()
synchronized Set keySet()
synchronized Enumeration keys()
synchronized V put(K key, V value)
synchronized void putAll(Map<? extends K, ? extends V> map)
synchronized V remove(Object key)
synchronized int size()
synchronized String toString()
synchronized Collection values()
(3)数据结构
(4)Hashtable遍历方式
import java.util.*;
/*
-
@desc 遍历Hashtable的测试程序。
-
(01) 通过entrySet()去遍历key、value,参考实现函数:
-
iteratorHashtableByEntryset()
-
(02) 通过keySet()去遍历key,参考实现函数:
-
iteratorHashtableByKeyset()
-
(03) 通过values()去遍历value,参考实现函数:
-
iteratorHashtableJustValues()
-
(04) 通过Enumeration去遍历key,参考实现函数:
-
enumHashtableKey()
-
(05) 通过Enumeration去遍历value,参考实现函数:
-
enumHashtableValue()
-
@author skywang
*/
public class HashtableIteratorTest {public static void main(String[] args) {
int val = 0;
String key = null;
Integer value = null;
Random r = new Random();
Hashtable table = new Hashtable();for (int i=0; i<12; i++) { // 随机获取一个[0,100)之间的数字 val = r.nextInt(100); key = String.valueOf(val); value = r.nextInt(5); // 添加到Hashtable中 table.put(key, value); System.out.println(" key:"+key+" value:"+value); } // 通过entrySet()遍历Hashtable的key-value iteratorHashtableByEntryset(table) ; // 通过keySet()遍历Hashtable的key-value iteratorHashtableByKeyset(table) ; // 单单遍历Hashtable的value iteratorHashtableJustValues(table); // 遍历Hashtable的Enumeration的key enumHashtableKey(table); // 遍历Hashtable的Enumeration的value //enumHashtableValue(table);
}
/*
-
通过Enumeration遍历Hashtable的key
-
效率高!
*/
private static void enumHashtableKey(Hashtable table) {
if (table == null)
return ;System.out.println("\nenumeration Hashtable");
Enumeration enu = table.keys();
while(enu.hasMoreElements()) {
System.out.println(enu.nextElement());
}
}
/*
-
通过Enumeration遍历Hashtable的value
-
效率高!
*/
private static void enumHashtableValue(Hashtable table) {
if (table == null)
return ;System.out.println("\nenumeration Hashtable");
Enumeration enu = table.elements();
while(enu.hasMoreElements()) {
System.out.println(enu.nextElement());
}
}
/*
-
通过entry set遍历Hashtable
-
效率高!
*/
private static void iteratorHashtableByEntryset(Hashtable table) {
if (table == null)
return ;System.out.println("\niterator Hashtable By entryset");
String key = null;
Integer integ = null;
Iterator iter = table.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();key = (String)entry.getKey(); integ = (Integer)entry.getValue(); System.out.println(key+" -- "+integ.intValue());
}
}
/*
-
通过keyset来遍历Hashtable
-
效率低!
*/
private static void iteratorHashtableByKeyset(Hashtable table) {
if (table == null)
return ;System.out.println("\niterator Hashtable By keyset");
String key = null;
Integer integ = null;
Iterator iter = table.keySet().iterator();
while (iter.hasNext()) {
key = (String)iter.next();
integ = (Integer)table.get(key);
System.out.println(key+" – "+integ.intValue());
}
}
/*
-
遍历Hashtable的values
*/
private static void iteratorHashtableJustValues(Hashtable table) {
if (table == null)
return ;Collection c = table.values();
Iterator iter= c.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
(5)示例
import java.util.*;
-
/*
-
@desc Hashtable的测试程序。
-
@author skywang
*/
public class HashtableTest {
public static void main(String[] args) {
testHashtableAPIs();
}private static void testHashtableAPIs() {
// 初始化随机种子
Random r = new Random();
// 新建Hashtable
Hashtable table = new Hashtable();
// 添加操作
table.put(“one”, r.nextInt(10));
table.put(“two”, r.nextInt(10));
table.put(“three”, r.nextInt(10));// 打印出table System.out.println("table:"+table ); // 通过Iterator遍历key-value Iterator iter = table.entrySet().iterator(); while(iter.hasNext()) { Map.Entry entry = (Map.Entry)iter.next(); System.out.println("next : "+ entry.getKey() +" - "+entry.getValue()); } // Hashtable的键值对个数 System.out.println("size:"+table.size()); // containsKey(Object key) :是否包含键key System.out.println("contains key two : "+table.containsKey("two")); System.out.println("contains key five : "+table.containsKey("five")); // containsValue(Object value) :是否包含值value System.out.println("contains value 0 : "+table.containsValue(new Integer(0))); // remove(Object key) : 删除键key对应的键值对 table.remove("three"); System.out.println("table:"+table ); // clear() : 清空Hashtable table.clear(); // isEmpty() : Hashtable是否为空 System.out.println((table.isEmpty()?"table is empty":"table is not empty") );
}