如果计算的哈希码超过INTEGER MAX LIMIT会发生什么?

AKh*_*AKh 4 java hash integer hashtable hashmap

这是来自Java HashTable Class的hashCode()实现.如果哈希表中的元素数量很大且哈希码超过INTEGER MAX LIMIT -2,147,483,648到2,147,483,647会怎么样?我假设hashCodes将是正整数.

 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;
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 11

我假设hashCodes将是正整数.

不,不一定.他们只是整数.它们肯定是负数,在计算哈希码时有整数溢出是好的.理想的哈希码将在其整个范围内均匀分布(int在本例中).使用哈希码的任何东西肯定需要考虑值为负的可能性.