Mohammad Yasin
Mohammad Yasin

Reputation: 149

non consistent java hashCode during multiple execution

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

Answers (1)

Hulk
Hulk

Reputation: 6573

Object.hashCode()

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

Related Questions