Java 基础 - 同一个String多次调用hashcode,返回值始终相等么?
总结
答案:不一定。不同的策略,hashcode返回值会不一样。
1-根据Object.hashcode()的注释,说明hashCode返回值与对象内存地址有一定关系。但只是其中一种策略而已。
2-根据c++ native hashcode()的源码,通过一个JVM启动参数-XX:hashCode,可以任意切换hashCode的生成策略:
- 与地址相关的生成策略有两条:
- hashCode==1:这种方式具有幂等的性质,在STW(stop-the-world)操作中,这种策略通常用于同步方案中。利用对象地址计算,使用不经常更新的随机数参与运算。
- hashCode==4:与创建对象的内存位置有关,原样输出。
- 其他情况:
- hashCode==0:简单地返回随机数,与对象的内存地址没有联系。然而根据随机数生成并全局地读写在多处理器下并不占优势。
- hashCode==2:始终返回完全相同的标识,即hashCode=1。这可用于测试依赖对象标识的代码。
- hashcode==3:从零开始计算哈希代码值。它看起来不是线程安全的,因此多个线程可以生成具有相同哈希代码的对象。
- hashCode>=5(默认):在jdk1.8中,这是默认的hashCode生成算法,支持多线程生成。使用了Marsaglia的xor-shift算法产生伪随机数。
c++ hashcode() 源码
static inline intptr_t get_next_hash(Thread * Self, oop obj) { intptr_t value = 0 ; if (hashCode == 0) { // This form uses an unguarded global Park-Miller RNG, // so it's possible for two threads to race and generate the same RNG. // On MP system we'll have lots of RW access to a global, so the // mechanism induces lots of coherency traffic. value = os::random() ; } else if (hashCode == 1) { // This variation has the property of being stable (idempotent) // between STW operations. This can be useful in some of the 1-0 // synchronization schemes. intptr_t addrBits = cast_from_oop<intptr_t>(obj) >> 3 ; value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ; } else if (hashCode == 2) { value = 1 ; // for sensitivity testing } else if (hashCode == 3) { value = ++GVars.hcSequence ; } else if (hashCode == 4) { value = cast_from_oop<intptr_t>(obj) ; } else { // Marsaglia's xor-shift scheme with thread-specific state // This is probably the best overall implementation -- we'll // likely make this the default in future releases. unsigned t = Self->_hashStateX ; t ^= (t << 11) ; Self->_hashStateX = Self->_hashStateY ; Self->_hashStateY = Self->_hashStateZ ; Self->_hashStateZ = Self->_hashStateW ; unsigned v = Self->_hashStateW ; v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)) ; Self->_hashStateW = v ; value = v ; }
参考文献
hashcode值一样对象一定相同吗_你所不知道的HashCode
分类:
Java 基础
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?