In a previous post i pointed out how questions posted in reward based discussions sites like stackoverflow.com never gets answered satisfactorily. This post is a look at one such feeble answer and makes an effort to explain in more detail a basic question about hashes.
The Question
In Java, the hash code for a
Stringobject is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]using
intarithmetic, wheres[i]is theith chara
Java hash 31
最新推荐文章于 2022-06-08 16:54:44 发布
本文探讨了在Java中,为何对象的哈希码计算时使用31作为乘数。31被选择是因为它是奇数质数,避免因乘法溢出导致信息丢失。有趣的是,31可以通过移位和减法来替代乘法,提高性能。现代虚拟机可能已经自动进行了这种优化。尽管有更复杂的哈希函数,但使用质数的传统依然存在。

最低0.47元/天 解锁文章
5400

被折叠的 条评论
为什么被折叠?



