gpt4 book ai didi

java - Java HashMap实现中的hash()方法有什么技巧?

转载 作者:太空狗 更新时间:2023-10-29 22:35:16 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Understanding strange Java hash function

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);
}

这个实现的算法原理我不是很明白。我可以引用任何解释或任何资源吗?谢谢!

更新

感谢大家的回答和资源。事实上,我理解哈希是如何工作的,但不知道为什么这段代码会确保 a bounded number of collisions,正如评论所说。有理论验证吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com