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

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

2024-09-19 23:27  views:174  source: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.



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

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