【问题标题】:By what factor does Java increase the size of Set / Map, if the size has reached the load factor如果大小已达到负载因子,Java 会通过什么因子增加 Set/Map 的大小
【发布时间】:2025-12-11 14:00:01
【问题描述】:

如果大小已达到负载因子,Java 会通过什么因子增加 Set / Map 的大小?我们得到原始 Set / Map 的两倍大小吗?

【问题讨论】:

  • 它变大了两倍的长度。请覆盖我的教程Internal life of HashMap以深入了解hasmap
  • @Volodymyr 精彩文章:-)

标签: java collections hash hashmap set


【解决方案1】:

来自JDK自带的源码:

void addEntry(int hash, K key, V value, int bucketIndex) {
    Entry<K,V> e = table[bucketIndex];
    table[bucketIndex] = new Entry<>(hash, key, value, e);
    if (size++ >= threshold)
        resize(2 * table.length);
}

【讨论】: