hashCode()值在两次执行之间发生变化 [英] hashCode() value changes between executions

查看:164
本文介绍了hashCode()值在两次执行之间发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hashCode 必须在应用程序的整个执行过程中与该对象保持一致- 对于任何 o对象,o.hashCode()应该返回相同的 int 值.

hashCode of an object has to be consistent for that object throughout one execution of the application-- for any object o, o.hashCode() should return the same int value.

但是,这不必是从一个运行时到另一个运行时: o.hashCode() 可以返回其他值,而规范.

However, this doesn't have to be from one run-time to another: o.hashCode() can return some other value and this is perfectly alright by the specs.

HashMap 根据 hashCode 值计算应得的权利.

HashMap calculates the right bucked based on the hashCode value.

我的问题是:这个值在一个会话到另一个会话之间如何变化? 序列化是否具有处理此问题的功能?

My Q is: how is this value change between one session to another handled? Does serialization have features to handle this?

因此,假设我建立了一个哈希并将其存储在磁盘上. 2周后,我调用了该应用程序, 运行它.我正在哈希中查找对象.通过这些, hashCode 这个对象现在/现在可以不同于以前 尽管它在哈希中,但我将无法找到它.

So, suppose I built a hash and stored it on the disk. 2 weeks after, I invoked the application and am running it. I'm looking up an object in the hash. By these, the hashCode of this object is/can be now different than before and I wont be able to find it although it is there in the hash.

推荐答案

之所以可行,是因为哈希表未序列化为哈希表.它们以自定义方式序列化.因此,在反序列化时,将使用新的哈希码重建哈希表.

This works because hashtables are not serialized as hashtables. They are serialized in a custom way. So when deserialized, the hashtable will be rebuilt using the new hashcodes.

这篇关于hashCode()值在两次执行之间发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆