JAVA中HashMap和Hashtable區別 .

來源:互聯網
上載者:User

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?
  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");  
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?
  1. a-key - a-value  
  2. c-key - c-value  
  3. b-key - b-value  
a-key - a-valuec-key - c-valueb-key - b-value

再看個Hashtable拒絕null的樣本:

[java]
view plaincopyprint?
  1. table.put(null, "a-value");  
table.put(null, "a-value");

運行之後異常如下:

[java]
view plaincopyprint?
  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)  
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?
  1. HashMap map = new HashMap();  
  2. map.put(null, "a-value");  
  3. map.put("b-key", null);  
  4. 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?
  1. b-key - null  
  2. null - a-value  
  3. c-key - null  
b-key - nullnull - a-valuec-key - null

 

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

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

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

Hashtable 和

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

[java]
view plaincopyprint?
  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. }  
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?
  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. }  
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?
  1. int hash = key.hashCode();  
  2. int index = (hash & 0x7FFFFFFF) % tab.length;  
int hash = key.hashCode();int index = (hash & 0x7FFFFFFF) % tab.length;

 

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

[java]
view plaincopyprint?
  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);  
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的指數。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.