HashMap和Hashtable的6个区别,最后一个没几个人知道!

开发 后端
HashMap 和 Hashtable 是 Java 开发程序员必须要掌握的,也是在各种 Java 面试场合中必须会问到的。但你对这两者的区别了解有多少呢?

HashMap和Hashtable的6 个区别,***一个没几个人知道!

HashMap 和 Hashtable 是 Java 开发程序员必须要掌握的,也是在各种 Java 面试场合中必须会问到的。

但你对这两者的区别了解有多少呢?

现在,栈长我给大家总结一下,或许有你不明朗的地方,在栈长的指点下都会拨开迷雾见晴天。

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、性能优劣

既然 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、实现方式

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、容量扩容

HashMap 的初始容量为:16,Hashtable 初始容量为:11,两者的负载因子默认都是: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 

当现有容量大于总容量 * 负载因子时,HashMap 扩容规则为当前容量翻倍,Hashtable 扩容规则为当前容量翻倍 + 1。

6、迭代器

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

所以,当其他线程改变了HashMap 的结构,如:增加、删除元素,将会抛出 ConcurrentModificationException 异常,而 Hashtable 则不会。

可以来看下这个区别的演示:

  1. /** 
  2. * 微信公众号:Java技术栈 
  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 的区别。 

责任编辑:庞桂玉 来源: Java技术栈
相关推荐

2020-06-12 16:10:58

进程线程Java

2019-10-08 15:08:23

互联网数据技术

2021-09-08 10:54:55

开源Linus创始人

2021-07-29 23:01:59

微信功能群聊

2020-05-14 18:25:40

微信移动应用群聊

2011-02-28 09:31:30

HashtableHashMap

2021-01-15 05:39:13

HashMapHashTableTreeMap

2021-11-05 07:59:25

HashMapJava知识总结

2014-05-20 09:54:20

2020-11-19 06:55:39

微信表情移动应用

2019-10-17 16:02:44

高并发缓存浏览器

2020-03-26 17:00:53

HashMapputJava

2021-11-02 14:54:41

排序数组元素

2019-11-12 12:34:15

人工智能机器学习技术

2021-12-13 11:31:36

排序数组数据结构算法

2012-06-15 14:58:01

诺基亚

2013-03-08 02:52:03

个人开发项目纠错

2015-06-12 15:29:06

一个人的爆品

2013-04-08 11:34:10

Ubuntu

2021-08-26 07:43:44

vectorerase错误
点赞
收藏

51CTO技术栈公众号