如果计算的散列码超过INTEGER MAX LIMIT,会发生什么情况? [英] What happens if hashcode calculated exceeds the INTEGER MAX LIMIT?
问题描述
以下是Java HashTable Class的hashCode()实现。如果散列表中元素的数量很大,散列码超过INTEGER MAX LIMIT -2,147,483,648至2,147,483,647,该怎么办?我假设hashCodes是正整数。
Here is the hashCode() implementation from Java HashTable Class. What if the number of elements in the hashtable is huge and the hashcode exceeds the INTEGER MAX LIMIT -2,147,483,648 to 2,147,483,647 ? I assume hashCodes will be positive integers.
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;
}
推荐答案
我假设hashCodes是正整数。
I assume hashCodes will be positive integers.
不,不一定。他们只是整数。它们肯定是负面的,在计算散列码时可以有整数溢出。理想的散列码将在整个范围内均匀分布(在这种情况下 int
)。任何使用的哈希代码都必须考虑到值为负值的可能性。
No, not necessarily. They're just integers. They can definitely be negative, and it's fine to have integer overflow while computing a hash code. An ideal hash code will be spread uniformly across the whole of its range (int
in this case). Anything using a hash code definitely needs to take into account the possibility of the value being negative.
这篇关于如果计算的散列码超过INTEGER MAX LIMIT,会发生什么情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!