首頁 文章 HashMap::hash {(h = key.hashCode()) ^ (h >>> 16)}

HashMap::hash {(h = key.hashCode()) ^ (h >>> 16)}

2024-09-19 23:27  瀏覽數:175  來源:redjujubee    

Computes key.hashCode() and spreads (XORs) higher bits of hash to lower. Because the
table uses power-of-two masking, sets of hashes that vary only in bits above the current
mask will always collide. (Among known examples are sets of Float keys holding consecutive
whole numbers in small tables.) So we apply a transform that spreads the impact of higher
bits downward. There is a tradeoff between speed, utility, and quality of bit-spreading.
Because many common sets of hashes are already reasonably distributed (so don't benefit
from spreading), and because we use trees to handle large sets of collisions in bins, we
just XOR some shifted bits in the cheapest possible way to reduce systematic lossage, as
well as to incorporate impact of the highest bits that would otherwise never be used in
index calculations because of table bounds.



聲明:以上文章均為用戶自行添加,僅供打字交流使用,不代表本站觀點,本站不承擔任何法律責任,特此聲明!如果有侵犯到您的權利,請及時聯系我們刪除。

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)