面试中被问到hashmap的rehash过程,有点搞晕了,rehash时get操作是怎么进行的? rehash过程中又有put操作 发生了什么? 可以帮忙解释一下吗?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
在 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 操作……完全不可预料会发生什么,有可能是很坏的事情,线程安全是个大问题。