The value of hashCode () changes between execution

The hashCode of the object must be consistent for this object in just one execution of the application-- for any object o, o.hashCode () must return the same int value .

However, this does not have to happen from one runtime to another: o.hashCode () can return some other value, and this is completely normal if in the specifications .

HashMap calculates the correct value based on the hashCode value .

My question is: how is the change in this value handled between sessions? Are there any functions in serialization for this?

So, suppose I created a hash and saved it to disk. After 2 weeks, I launched the application and run it. I am looking for an object in a hash. By this, the hashCode from this object may now be different than before and I will not be able to find it, although it is in the hash.

+4
source share
3 answers

This works because hashtables are not serialized as hashtables. They are serialized in their own way. Therefore, during deserialization, the hash table will be rebuilt using the new hash codes.

+7
source

The general contract Object.hashCode(), as you say, indicates that the hash code can vary between runs.

, -, , hashCode() , .

, Object.hashCode(). hashCode(), , - . , hashCode() , - , .

, , , , , . , . hashCode(), - -, , , (.. Integer, ).

, Java HashMap , - . , - - , , - (, , hashCode()).

+2

-, , -. - - .

+1

All Articles