0
点赞
收藏
分享

微信扫一扫

【Java-笔试面试】HashMap 、HashTable、ConcurrentHashMap有什么区别?

狐沐说 2022-02-01 阅读 49
  1. 安全性:HashMap 不是线程安全的,HashTable 是线程安全的,HashTable 是通过在方法上使用 synchronized 关键字实现线程安全的。如果要使用线程安全的 Map 的话应该使用 ConcurrentHashMap,他使用了 synchronized + CAS 来操作,效率比 HashTable高很多。
  2. 效率:HashMap > ConcurrentHashMap>HashTable
  3. 数据结构:HashMap = ConcurrentHashMap,数组+链表+红黑树,HashTable 是数组+链表。
  4. 扩容:HashTable 初始是 11,扩容是 2n + 1。HashMap、ConcurrentHashMap 初始是16,负载因子是 0.75,扩容为原来的两倍。
  5. 【补充】负载因子为什么是 0.75 ?为了让 hash 桶中的对象个数服从参数为 0.5 的 Poisson 分布。
举报

相关推荐

0 条评论