| 維度 | HashMap | Hashtable |
|---|---|---|
| 繼承體系 | AbstractMap | Dictionary(舊集合類) |
| 線程安全 | ❌ 非同步 | ✅ 所有方法 synchronized |
| Null Key | ✅ 允許一個 | ❌ 不允許 |
| Null Value | ✅ 允許多個 | ❌ 不允許 |
| 迭代器 | Fail-fast | Enumerator(非 fail-fast) |
| 性能 | 高(無鎖) | 低(全鎖) |
| 推薦替代 | 單線程首選 | 已過時,用 ConcurrentHashMap |
ConcurrentHashMap(JDK 8 CAS + synchronized + 紅黑樹)HashMap 非線程安全,多線程下可能導致死循環(擴容鏈表成環)或數據丟失HashMap:Collections.synchronizedMap(new HashMap<>()) 或 ConcurrentHashMap新代碼不應使用 Hashtable。單線程用 HashMap,多線程用 ConcurrentHashMap,這是現代 Java 開發的標準選擇。