【发布时间】:2020-01-09 03:31:46
【问题描述】:
我正在阅读这里提出的问题的答案: Why does hashcode() returns an integer and not long?
我的问题是:为什么基于哈希码的数据结构使用数组来创建 bin?
【问题讨论】:
-
由于 hashCode() 的主要用途是确定将对象插入到哪个槽中,因此数组被证明是具有恒定随机访问的最简单的“低级”数据结构。
-
数组元素的索引访问是一个很小的常数成本:O(1),因此
array[hashcode % array,length]是理想的。一棵树需要 O(log n)。
标签: data-structures hashmap hashset