自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HashMap和Hashtable的6個區(qū)別,最后一個沒幾個人知道!

開發(fā) 后端
HashMap 和 Hashtable 是 Java 開發(fā)程序員必須要掌握的,也是在各種 Java 面試場合中必須會問到的。但你對這兩者的區(qū)別了解有多少呢?

HashMap和Hashtable的6 個區(qū)別,***一個沒幾個人知道!

HashMap 和 Hashtable 是 Java 開發(fā)程序員必須要掌握的,也是在各種 Java 面試場合中必須會問到的。

但你對這兩者的區(qū)別了解有多少呢?

現(xiàn)在,棧長我給大家總結(jié)一下,或許有你不明朗的地方,在棧長的指點下都會撥開迷霧見晴天。

1、線程安全

Hashtable 是線程安全的,HashMap 不是線程安全的。

為什么說 HashTable 是線程安全的?

來看下 Hashtable 的源碼,Hashtable 所有的元素操作都是 synchronized 修飾的,而 HashMap 并沒有。

  1. public synchronized V put(K key, V value); 
  2. public synchronized V get(Object key); 
  3. ... 

2、性能優(yōu)劣

既然 Hashtable 是線程安全的,每個方法都要阻塞其他線程,所以 Hashtable 性能較差,HashMap 性能較好,使用更廣。

如果要線程安全又要保證性能,建議使用 JUC 包下的 ConcurrentHashMap。

3、NULL

Hashtable 是不允許鍵或值為 null 的,HashMap 的鍵值則都可以為 null。

那么問題來了,為什么 Hashtable 是不允許 KEY 和 VALUE 為 null, 而 HashMap 則可以?

Hashtable put 方法邏輯:

  1. public synchronized V put(K key, V value) { 
  2.         // Make sure the value is not null 
  3.         if (value == null) { 
  4.             throw new NullPointerException(); 
  5.         } 
  6.  
  7.         // Makes sure the key is not already in the hashtable. 
  8.         Entry<?,?> tab[] = table
  9.         int hash = key.hashCode(); 
  10.  
  11.         ... 
  12.  
  13. }         

HashMap hash 方法邏輯:

  1. static final int hash(Object key) { 
  2.     int h; 
  3.     return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); 

可以看出 Hashtable key 為 null 會直接拋出空指針異常,value 為 null 手動拋出空指針異常,而 HashMap 的邏輯對 null 作了特殊處理。

4、實現(xiàn)方式

Hashtable 的繼承源碼:

  1. public class Hashtable<K,V> 
  2.     extends Dictionary<K,V> 
  3.     implements Map<K,V>, Cloneable, java.io.Serializable 

HashMap 的繼承源碼:

  1. public class HashMap<K,V> extends AbstractMap<K,V> 
  2.     implements Map<K,V>, Cloneable, Serializable 

可以看出兩者繼承的類不一樣,Hashtable 繼承了 Dictionary類,而 HashMap 繼承的是 AbstractMap 類。

Dictionary 是 JDK 1.0 添加的,貌似沒人用過這個,棧長我也沒用過。。

5、容量擴(kuò)容

HashMap 的初始容量為:16,Hashtable 初始容量為:11,兩者的負(fù)載因子默認(rèn)都是:0.75。

  1. /** 
  2.  * Constructs a new, empty hashtable with a default initial capacity (11) 
  3.  * and load factor (0.75). 
  4.  */ 
  5. public Hashtable() { 
  6.     this(11, 0.75f); 
  7.  
  8. /** 
  9.  * Constructs an empty <tt>HashMap</tt> with the default initial capacity 
  10.  * (16) and the default load factor (0.75). 
  11.  */ 
  12. public HashMap() { 
  13.     this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted 

當(dāng)現(xiàn)有容量大于總?cè)萘?* 負(fù)載因子時,HashMap 擴(kuò)容規(guī)則為當(dāng)前容量翻倍,Hashtable 擴(kuò)容規(guī)則為當(dāng)前容量翻倍 + 1。

6、迭代器

HashMap 中的 Iterator 迭代器是 fail-fast 的,而 Hashtable 的 Enumerator 不是 fail-fast 的。

所以,當(dāng)其他線程改變了HashMap 的結(jié)構(gòu),如:增加、刪除元素,將會拋出 ConcurrentModificationException 異常,而 Hashtable 則不會。

可以來看下這個區(qū)別的演示:

  1. /** 
  2. * 微信公眾號:Java技術(shù)棧 
  3. **/ 
  4. public static void main(String[] args) { 
  5.     Map<String, String> hashtable = new Hashtable<>(); 
  6.     hashtable.put("t1""1"); 
  7.     hashtable.put("t2""2"); 
  8.     hashtable.put("t3""3"); 
  9.  
  10.     Enumeration<Map.Entry<String, String>> iterator1 = (Enumeration<Map.Entry<String, String>>) hashtable.entrySet().iterator(); 
  11.     hashtable.remove(iterator1.nextElement().getKey()); 
  12.     while (iterator1.hasMoreElements()) { 
  13.         System.out.println(iterator1.nextElement()); 
  14.     } 
  15.  
  16.     Map<String, String> hashMap = new HashMap<>(); 
  17.     hashMap.put("h1""1"); 
  18.     hashMap.put("h2""2"); 
  19.     hashMap.put("h3""3"); 
  20.  
  21.     Iterator<Map.Entry<String, String>> iterator2 = hashMap.entrySet().iterator(); 
  22.     hashMap.remove(iterator2.next().getKey()); 
  23.     while (iterator2.hasNext()) { 
  24.         System.out.println(iterator2.next()); 
  25.     } 
  26.  

輸出信息:

  1. t2=2 
  2. t1=1 
  3. Exception in thread "main" java.util.ConcurrentModificationException 
  4.     at java.util.HashMap$HashIterator.nextNode(HashMap.java:1442) 
  5.     at java.util.HashMap$EntryIterator.next(HashMap.java:1476) 
  6.     at java.util.HashMap$EntryIterator.next(HashMap.java:1474) 
  7.     at cn.javastack.Test.main(Test.java:37) 

看到了吧?

所以,這條同樣也是 Enumeration 和 Iterator 的區(qū)別。 

責(zé)任編輯:龐桂玉 來源: Java技術(shù)棧
相關(guān)推薦

2020-06-12 16:10:58

進(jìn)程線程Java

2019-10-08 15:08:23

互聯(lián)網(wǎng)數(shù)據(jù)技術(shù)

2021-09-08 10:54:55

開源Linus創(chuàng)始人

2021-07-29 23:01:59

微信功能群聊

2024-09-04 15:17:23

2011-02-28 09:31:30

HashtableHashMap

2020-05-14 18:25:40

微信移動應(yīng)用群聊

2021-01-15 05:39:13

HashMapHashTableTreeMap

2021-11-05 07:59:25

HashMapJava知識總結(jié)

2019-10-17 16:02:44

高并發(fā)緩存瀏覽器

2020-11-19 06:55:39

微信表情移動應(yīng)用

2014-05-20 09:54:20

2020-03-26 17:00:53

HashMapputJava

2021-11-02 14:54:41

排序數(shù)組元素

2025-03-07 09:18:10

2021-12-13 11:31:36

排序數(shù)組數(shù)據(jù)結(jié)構(gòu)算法

2013-03-08 02:52:03

個人開發(fā)項目糾錯

2019-11-12 12:34:15

人工智能機(jī)器學(xué)習(xí)技術(shù)

2012-06-15 14:58:01

諾基亞

2015-06-12 15:29:06

一個人的爆品
點贊
收藏

51CTO技術(shù)棧公眾號