Hashtable與HashMap的區別

轉自:http://blog.csdn.net/tianfeng701/article/details/7588091

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 plain copy
  1. Hashtable table = new Hashtable();  
  2. table.put("a-key""a-value");  
  3. table.put("b-key""b-value");  
  4. table.put("c-key""c-value");  

輸出如下:

[java] view plain copy
  1. a-key - a-value  
  2. c-key - c-value  
  3. b-key - b-value  

再看個Hashtable拒絕null的示例:

[java] view plain copy
  1. table.put(null"a-value");  

運行之後異常如下:

[java] view plain copy
  1. Exception in thread "main" java.lang.NullPointerException  
  2. at java.util.Hashtable.put(Hashtable.java:399)  
  3. at com.darkmi.sandbox.HashtableTest.main(HashtableTest.java:20)  

HashMap示例:

[java] view plain copy
  1. HashMap map = new HashMap();  
  2. map.put(null"a-value");  
  3. map.put("b-key"null);  
  4. map.put("c-key"null);  


 

運行之後,輸出如下:

[java] view plain copy
  1. b-key - null  
  2. null - a-value  
  3. c-key - null  


 

PS:從上面的示例我們倒是可以發現Hashtable與HashMap相同的一點:無序存放。

3.兩者的遍歷方式大同小異,Hashtable僅僅比HashMap多一個elements方法。

[java] view plain copy
  1. Enumeration em = table.elements();  
  2. while (em.hasMoreElements()) {  
  3. String obj = (String) em.nextElement();  
  4. System.out.println(obj);   
  5. }  


Hashtable 和 HashMap 都能通過values()方法返回一個 Collection ,然後進行遍歷處理:

[java] view plain copy
  1. Collection coll = map.values();  
  2. Iterator it = coll.iterator();  
  3. while (it.hasNext()) {  
  4. String obj = (String) it.next();  
  5. System.out.println(obj);  
  6. }  


 

兩者也都可以通過 entrySet() 方法返回一個 Set , 然後進行遍歷處理:

[java] view plain copy
  1. Set set = table.entrySet();  
  2. Iterator it = set.iterator();  
  3. while (it.hasNext()) {  
  4. Entry entry = (Entry) it.next();  
  5. System.out.println(entry.getKey() + " - " + entry.getValue());  
  6.   
  7. }  

 

4.HashTable使用Enumeration,HashMap使用Iterator

 

以下這兩點是從內部實現機制上來進行比較,

瞭解即可:

5.哈希值的使用不同,Hashtable直接使用對象的hashCode,代碼是這樣的:

[java] view plain copy
  1. int hash = key.hashCode();  
  2. int index = (hash & 0x7FFFFFFF) % tab.length;  

 

而HashMap重新計算hash值,而且用與代替求模:

[java] view plain copy
  1. int hash = hash(k);  
  2. int i = indexFor(hash, table.length);  
  3.   
  4. static int hash(Object x) {  
  5.   int h = x.hashCode();  
  6.   
  7.   h += ~(h << 9);  
  8.   h ^= (h >>> 14);  
  9.   h += (h << 4);  
  10.   h ^= (h >>> 10);  
  11.   return h;  
  12. }  
  13.   
  14. static int indexFor(int h, int length) {  
  15.   return h & (length-1);  


 

6.Hashtable中hash數組默認大小是11,增加的方式是 old*2+1。HashMap中hash數組的默認大小是16,而且一定是2的指數。

發佈了4 篇原創文章 · 獲贊 6 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章