/**
* Applies a supplemental hash function to a given hashCode, which
* defends against poor quality hash functions. This is critical
* because HashMap uses power-of-two length hash tables, that
* otherwise encounter collisions for hashCodes that do not differ
* in lower bits. Note: Null keys always map to hash 0, thus index 0.
*/
static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20)^(h >>> 12);
return h^(h >>> 7)^(h >>> 4);
}
誰能解釋詳細此方法,謝謝。
來源評論相當詳細。你想了解什麼?目的?位移邏輯?別的東西? – Fredrik
@Foredoomed我可以說這只是一堆XOR環和位移位,但其背後的邏輯可能更爲複雜。 – Eugene
我認爲這是這個問題的重複 –