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

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

2024-09-19 23:27  浏览数:173  来源: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)