关于 HashTable

  • HashTable 是 Dictionary 的子类;
  • HashTable 是线程安全的;
  • HashTable 的键( key )和值( value )都不能为 null;
  • HashTable 的使用方法基本上和 HashMap 一样;
  • 如果需要线程安全的并发实现,那么建议使用 ConcurrentHashMap 代替 Hashtable;
  • HashTable 是 JDK 1.0 引入的;
  1. public class Hashtable<K,V>
  2. extends Dictionary<K,V>
  3. implements Map<K,V>, Cloneable, java.io.Serializable

构造器

image.png

源码分析

问题

HashTable 与 HashMap

HashTable 不允许 null 键/值,但 HashMap 允许。
HashTable 是线程安全的,但 HashMap 不是。
HashTable 比 HashMap 效率低。
HashTable 是 JDK 1.0 引入的,HashMap 是JDK 1.2 引入的。