Reputation: 149
Why would hashCode in java need not to be consistent during multiple execution? I want to understand if there is a reason in background!
From Object class JavaDoc:
This integer need not remain consistent from one execution of an application to another execution of the same application.
Upvotes: 0
Views: 196
Reputation: 6573
This method is supported for the benefit of hash tables such as those provided by HashMap.
It is not required that it remains consistent across executions because that property is not needed to implement hash tables. It may be easier to implement it in a way that does not provide such guarantees. The contract for this method gives the implementation a lot of freedom to choose an efficient implementation, without imposing more restrictions than necessary.
Upvotes: 1