gpt4 book ai didi

java - map 负载因子, map 如何增长

转载 作者:行者123 更新时间:2023-12-01 23:25:16 25 4
gpt4 key购买 nike

根据我的理解和我读过的内容

The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased

因此,当负载因子为 0.8(80%)、 map 大小为 10 时,当 Map 中放入 8 个元素时,Map 的大小将增加 10。

所以,现在 Map 的大小为 20。我的疑问是下一个 10 个元素空间何时会添加到 Map 中。

  • Map 再次充满 80% 时,即在 Map 中放入 16 个元素时。

  • Map 中放入 18 个元素时。

最佳答案

那将是 16。如果您查看 HashMap 的 java 代码:

threshold = (int)(newCapacity * loadFactor);

其中新容量是新大小。因此,您的示例中的限制将为 16。

关于java - map 负载因子, map 如何增长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20101999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com