I wanted to ask:
And just like for equals (), there are official requirements for the hashCode () method that are written in the Oracle documentation:
If the two objects are equal (i.e., the equals () method returns true), they must have the same hash code.
Otherwise, our methods will be meaningless. The check by hashCode (), as we said, should go first to improve performance. If the hash codes are different, the check returns false, although the objects are actually equal (according to our definition in the equals () method).
If the hashCode () method is called several times on the same object, each time it must return the same number.
Rule 1 does not work in the opposite direction. The same hash code can be in two different objects.
I'm confused to help figure out:
Why does rule 3 say that rule 1 does not work in the opposite direction?
It turns out 1 rule 2 objects are equal and their hash code is the same.
A rule 3 is the same hash code for 2 objects or I do not really understand? Why in 1 rule it is written if 2 objects are equal, and in rule 3 the same hash code is written for two DIFFERENT objects, how to understand different?
Integer
type range. Based on this, the number of different objects may be greater than the number of different hash codes, which means that different objects may have the same hash code (a so-called collision). The hash code is checked first, and thenequals
. If the hash codes are different, then the objects are guaranteed to be different and there is no point in checking forequals
. If the hash codes are the same, then you need to check forequals
. - post_zeewBigInteger
?) - Anton Sorokin