【发布时间】:2016-03-18 19:50:56
【问题描述】:
我正在经历Rehashing process in hashmap or hashtable 这解释了美丽的概念 HashMap 重新散列。 我有一个问题。
假设 HashMap 的初始容量为 16,负载因子为 0.75。现在,12 个元素被添加到 HashMap 中,但由于 hashCode 实现不佳,它们最终都在同一个桶中。其他 15 个桶不包含任何元素。 HashMap 会重新散列吗?
【问题讨论】:
我正在经历Rehashing process in hashmap or hashtable 这解释了美丽的概念 HashMap 重新散列。 我有一个问题。
假设 HashMap 的初始容量为 16,负载因子为 0.75。现在,12 个元素被添加到 HashMap 中,但由于 hashCode 实现不佳,它们最终都在同一个桶中。其他 15 个桶不包含任何元素。 HashMap 会重新散列吗?
【问题讨论】:
实际上,至少在 HashMap 的当前实现中,冲突不会影响重新散列。例如,参见lines 661-662 末尾的HashMap.putVal,它只是检查
if (++size > threshold)
resize();
其中threshold is (capacity * load factor),缓存在一个字段中以提高速度(避免浮点运算)。
HashMap 处理过满的桶by converting the buckets from linked lists to balanced trees。
【讨论】: