I wanted to ask:

And just like for equals (), there are official requirements for the hashCode () method that are written in the Oracle documentation:

  1. 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).

  1. If the hashCode () method is called several times on the same object, each time it must return the same number.

  2. 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?

  • one
    The whole point is that the set of objects is limited only by your imagination, and the set of hash codes is limited by the 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 then equals . If the hash codes are different, then the objects are guaranteed to be different and there is no point in checking for equals . If the hash codes are the same, then you need to check for equals . - post_zeew
  • @post_zeew and if you use BigInteger ?) - Anton Sorokin

1 answer 1

Note: it is assumed that the hashCode() method is hashCode() in the class.

On good, different objects should have a different hashcode. But in practice it sometimes happens differently. Very often this is due to the imperfection of the formula for calculating the hashcode.

Example: a string hash is calculated by the length of the string: length*3 . Then the strings foo and bar same hashes.


In general, the hashcode is used so that you can say for sure that the objects are different. But not to say that they are the same. The same hashcode is not a guarantee for identical objects.


It is usually used to compare objects:
Suppose you have an object in which there are many, many fields. In most cases, the objects for comparison will be unequal. In order not to compare a bunch of variables (if the objects are not equal), you can first compare the hashcode (because if the hash is different, then the objects are exactly different). If the hashes are different, you can no longer compare the variables. If the same - then you need to compare the variables (because, if the hashes are the same, then this does not mean that the objects are the same).


Well and the last - almost always the returned type of the hashCode() method hashCode() - int . The int has a certain limit (from -21 ... to +21 ... if I'm not mistaken). If there are more objects than this limit, it is physically impossible to generate different hashes for all objects. That is, when using hashes, you can increase the performance of the program.


I will try to explain the rules:

  • Same objects always have the same hashes.
  • The same object must always have an unchangeable hashcode (unless the values ​​inside the object have changed)
  • Different objects can sometimes have the same hashes.
  • And that is, in my first rule where I wrote the question, do they have the same objects always have the same hashes as I understand? - Petrovchenko Ivan
  • @ Petrovchenko Ivan yes - Anton Sorokin
  • @AntonSorokin default - different - Stranger in the Q
  • @StrangerintheQ? - Anton Sorokin
  • А то есть в моём первом правило где я написал в вопросе, там имеют ввиду у одинаковых объектов всегда одинаковые хэши как я понял? – Петровченко Иван А то есть в моём первом правило где я написал в вопросе, там имеют ввиду у одинаковых объектов всегда одинаковые хэши как я понял? – Петровченко Иван @ПетровченкоИван да – Anton Sorokin - Stranger in the Q