这里是Java HashTable类中的hashCode()实现。如果哈希表中的元素数量很大,而哈希码超过了整数最大值-2,147,483,648到2,147,483,647怎么办?我假设哈希码将是正整数。
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;
}
System.out.println("Are hashCodes always positive?".hashCode());
输出-835520151
;) ,意思是“哈希码总是正数吗?” - Peter Lawrey