java - hashmap rehash过程
高洛峰
高洛峰 2017-04-17 14:54:58
[Java讨论组]

面试中被问到hashmap的rehash过程,有点搞晕了,rehash时get操作是怎么进行的? rehash过程中又有put操作 发生了什么? 可以帮忙解释一下吗?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(1)
伊谢尔伦

在 HashMap (Java Platform SE 8) 文档中有提到

When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.

简单说 rehash 就是因为 HashMap 的空间不够用了,所以需要分配一个大一点的空间,然后保存在里面的内容需要重新计算 hash。

HashMap 不是线程安全的,所以在 rehash 的过程中又有 put 操作……完全不可预料会发生什么,有可能是很坏的事情,线程安全是个大问题。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号