Why can different objects have the same hash code?


I wanted to ask:

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

  1. If 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 hashCode () check, as we said, should go first to improve performance. If the hash codes are different, the check returns false, even though the objects are actually equal (according to our definition in the equals () method).

  1. If the hashCode() method is called multiple times on the same for example, it should return the same number each time.

  2. Rule 1 doesn't work the other way around. The same hash code can be be at two different objects.

I'm confused help me figure it out:
Why is the rule 3 written like rule 1 doesn't work the other way around?
It turns out that 1 rule 2 objects are equal and they have the same hash code.
And the 3rd rule is also the same hash code for 2 objects, or do I not really understand? Why is the rule written in 1 if 2 objects are equal, and in 3 the rule is written the same hash code can be in two DIFFERENT objects, how to understand different ones?

Author: Sergey Gornostaev, 2019-05-11

1 answers

Note: it is assumed that the method is overridden in the classhashCode().

In a good way, different objects should have different hashcodes. But in practice, sometimes it happens differently. Very often, this is due to an imperfect formula for calculating the hashcode.

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


In general, the hashcode is used to be able to say exactly that the objects are different. But not to say that they are the same. The same hashcode is not a guarantee of identical objects.


It is usually used to compare objects:
Let's say you have an object that has many, many fields. In most cases, the objects to compare will be unequal. To avoid comparing 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 then do not compare the variables. If they are the same, then you need to compare the variables (because if the hashes are the same, it does not mean that the objects are the same).


And the last thing - almost always the return type of the method hashCode() - int. int has a certain limit(from -21... up to +21..., if I'm not mistaken). If there are more different objects than this limit, then it is physically impossible to generate different hashes for all objects. I.e., using hashes can increase performance programs.


I'll try to explain the rules:

  • Identical objects always have the same hashes
  • The same object must always have an immutable hashcode(if the values inside the object have not changed)
  • Different objects can sometimes have the same hashes
 5
Author: Anton Sorokin, 2019-05-11 11:53:12