gpt4 book ai didi

java - HashMap 初始化参数(load/initialcapacity)

转载 作者:IT老高 更新时间:2023-10-28 20:51:04 25 4
gpt4 key购买 nike

我应该传递什么值来为 N 个项目创建一个基于 HashMap/HashMap 的高效结构?

ArrayList 中,有效数字是 N(N 已经假设 future 增长)。 HashMap 的参数应该是什么? ((int)(N * 0.75d), 0.75d)?更多的?较少的?改变负载率有什么影响?

最佳答案

关于负载系数,我将简单地引用 HashMap javadoc :

As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.

意思是,负载因子不应该从 .75 更改,除非您要进行一些特定的优化。初始容量是您唯一要更改的内容,并根据您的 N 值进行设置 - 表示 (N/0.75) + 1 或该区域的内容。这将确保表始终足够大并且不会发生重新散列。

关于java - HashMap 初始化参数(load/initialcapacity),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/434989/

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