栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

有效Java hashCode()实现中的位移

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

有效Java hashCode()实现中的位移

基本上,它对long的高32位与低32位进行异或。这是分解版本:

// Unsigned shift by 32 bits, so top 32 bits of topBits will be 0,// bottom 32 bits of topBits will be the top 32 bits of llong topBits = l >>> 32;// XOR topBits with l; the top 32 bits will effectively be left// alone, but that doesn't matter because of the next step. The// bottom 32 bits will be the XOR of the top and bottom 32 bits of llong xor = l ^ topBits;// Convert the long to an int - this basically ditches the top 32 bitsint hash = (int) xor;

回答您的评论:您有一个long值,必须将其转换为int才能作为哈希的一部分(结果必须仅为32位)。你打算怎么做?您 可以 只使用低32位-但这意味着
高32位的更改将被忽略,这不会使其成为一个很好的哈希。这样,输入的单个位的改变 总是 导致哈希的单个位的改变。诚然,您仍然可以轻松地发生冲突-
例如,将第7位和第39位 更改,或者将其他任何一对32位位置分开-但是,既然您将要从2 64个可能的值更改为2 32。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/399935.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号