Hashtable的應用非常廣泛,HashMap是新架構中用來代替Hashtable的類,也就是說建議使用HashMap,不要使用Hashtable。可能你覺得Hashtable很好用,為什麼不用呢?這裡簡單分析他們的區別。
1.Hashtable的方法是同步的,HashMap未經同步,所以在多線程場合要手動同步HashMap這個區別就像Vector和ArrayList一樣。
查看Hashtable的原始碼就可以發現,除建構函式外,Hashtable的所有 public 方法聲明中都有 synchronized 關鍵字,而HashMap的原始碼中則連 synchronized 的影子都沒有,當然,注釋除外。
2.Hashtable不允許 null 值(key 和 value 都不可以),HashMap允許 null 值(key和value都可以)。
先看個Hashtable正常輸出的樣本:
[java]
view plaincopyprint?
- Hashtable table = new Hashtable();
- table.put("a-key", "a-value");
- table.put("b-key", "b-value");
- table.put("c-key", "c-value");
Hashtable table = new Hashtable();table.put("a-key", "a-value");table.put("b-key", "b-value");table.put("c-key", "c-value");
輸出如下:
[java]
view plaincopyprint?
- a-key - a-value
- c-key - c-value
- b-key - b-value
a-key - a-valuec-key - c-valueb-key - b-value
再看個Hashtable拒絕null的樣本:
[java]
view plaincopyprint?
- table.put(null, "a-value");
table.put(null, "a-value");
運行之後異常如下:
[java]
view plaincopyprint?
- Exception in thread "main" java.lang.NullPointerException
- at java.util.Hashtable.put(Hashtable.java:399)
- at com.darkmi.sandbox.HashtableTest.main(HashtableTest.java:20)
Exception in thread "main" java.lang.NullPointerExceptionat java.util.Hashtable.put(Hashtable.java:399)at com.darkmi.sandbox.HashtableTest.main(HashtableTest.java:20)
HashMap樣本:
[java]
view plaincopyprint?
- HashMap map = new HashMap();
- map.put(null, "a-value");
- map.put("b-key", null);
- map.put("c-key", null);
HashMap map = new HashMap();map.put(null, "a-value");map.put("b-key", null);map.put("c-key", null);
運行之後,輸出如下:
[java]
view plaincopyprint?
- b-key - null
- null - a-value
- c-key - null
b-key - nullnull - a-valuec-key - null
PS:從上面的樣本我們倒是可以發現Hashtable與HashMap相同的一點:無序存放。
3.兩者的遍曆方式大同小異,Hashtable僅僅比HashMap多一個elements方法。
[java]
view plaincopyprint?
- Enumeration em = table.elements();
- while (em.hasMoreElements()) {
- String obj = (String) em.nextElement();
- System.out.println(obj);
- }
Enumeration em = table.elements();while (em.hasMoreElements()) {String obj = (String) em.nextElement();System.out.println(obj); }
Hashtable 和
HashMap 都能通過values()方法返回一個 Collection ,然後進行遍曆處理:
[java]
view plaincopyprint?
- Collection coll = map.values();
- Iterator it = coll.iterator();
- while (it.hasNext()) {
- String obj = (String) it.next();
- System.out.println(obj);
- }
Collection coll = map.values();Iterator it = coll.iterator();while (it.hasNext()) {String obj = (String) it.next();System.out.println(obj);}
兩者也都可以通過 entrySet() 方法返回一個 Set , 然後進行遍曆處理:
[java]
view plaincopyprint?
- Set set = table.entrySet();
- Iterator it = set.iterator();
- while (it.hasNext()) {
- Entry entry = (Entry) it.next();
- System.out.println(entry.getKey() + " - " + entry.getValue());
-
- }
Set set = table.entrySet();Iterator it = set.iterator();while (it.hasNext()) {Entry entry = (Entry) it.next();System.out.println(entry.getKey() + " - " + entry.getValue());}
4.HashTable使用Enumeration,HashMap使用Iterator
以下這兩點是從內部實現機制上來進行比較,
瞭解即可:
5.雜湊值的使用不同,Hashtable直接使用對象的hashCode,代碼是這樣的:
[java]
view plaincopyprint?
- int hash = key.hashCode();
- int index = (hash & 0x7FFFFFFF) % tab.length;
int hash = key.hashCode();int index = (hash & 0x7FFFFFFF) % tab.length;
而HashMap重新計算hash值,而且用與代替求模:
[java]
view plaincopyprint?
- int hash = hash(k);
- int i = indexFor(hash, table.length);
-
- static int hash(Object x) {
- int h = x.hashCode();
-
- h += ~(h << 9);
- h ^= (h >>> 14);
- h += (h << 4);
- h ^= (h >>> 10);
- return h;
- }
-
- static int indexFor(int h, int length) {
- return h & (length-1);
int hash = hash(k);int i = indexFor(hash, table.length);static int hash(Object x) { int h = x.hashCode(); h += ~(h << 9); h ^= (h >>> 14); h += (h << 4); h ^= (h >>> 10); return h;}static int indexFor(int h, int length) { return h & (length-1);
6.Hashtable中hash數組預設大小是11,增加的方式是 old*2+1。HashMap中hash數組的預設大小是16,而且一定是2的指數。