这是Java HashTable Class的hashCode()实现。如果散列表中元素的数量很大,散列码超过INTEGER MAX LIMIT -2,147,483,648至2,147,483,647,该怎么办?我假设hashCodes将是正整数。如果计算的散列码超过INTEGER MAX LIMIT,会发生什么情况?
public synchronized int hashCode() {
int h = 0;
if (count == 0 || loadFactor < 0)
return h; // Returns zero
loadFactor = -loadFactor; // Mark hashCode computation in progress
Entry[] tab = table;
for (int i = 0; i < tab.length; i++)
for (Entry e = tab[i]; e != null; e = e.next)
h += e.key.hashCode()^e.value.hashCode();
loadFactor = -loadFactor; // Mark hashCode computation complete
return h;
}
高于int类型限制(32位)的位将被丢弃。 – nhahtdh
“如果散列表中元素的数量很大”呢?它是什么 - 哈希表必须处理碰撞。没有要求,也不保证哈希码是唯一的(事实上,不可能有这样的保证) –
'的System.out.println(“是否散列码总是积极?”的hashCode());''打印-835520151';) –