来自javadochttps://docs.oracle.com/javase/8/docs/api/java/util/hashmap.html#put-k-v公司- 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. 因此,hashmap在有12个键值对时调整大小。这并不奇怪,因为在调整条目大小之后,它们的bucket会发生变化。
2条答案
按热度按时间1qczuiv01#
来自javadochttps://docs.oracle.com/javase/8/docs/api/java/util/hashmap.html#put-k-v公司-
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.
因此,hashmap在有12个键值对时调整大小。这并不奇怪,因为在调整条目大小之后,它们的bucket会发生变化。new9mtju2#
如本链接所述。
它表示hashmap的第12个键值对将其大小保持为16。一旦第13个元素(键值对)进入hashmap,它就会将其大小从默认的2^4=16个bucket增加到2^5=32个bucket。
独立于每个键插入的位置,当负载系数和当前容量的乘积超过时,表将被调整大小。
hashmap不关心在达到负载因子之前使用了多少个bucket,它知道发生冲突的概率变得太大,应该调整map的大小。尽管已经发生了很多碰撞。