In Java, the equals and hashCode methods are methods of the Object class, so all user-written classes have them by default.

The equals method

The equals method is used to compare two objects for equality.

In the Object class, equals by default evaluates two objects using the “==” number. In Java, equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals

This way of judging essentially that’s right, but not too conforms to the actual demand, just as in two different supermarkets have mineral water, but because the address values are different, when use the equals to do judgment, the two supermarket mineral water will be returned to false; Therefore, in real development we often need to override the Equals method of Object.

What the hashCode method does

When the hashCode method is not overridden, it mainly returns an integer hash value based on the current object. Different objects often return different values when calling hashCode.

In Java’s underlying collection framework, the hashCode method is often used to determine the location of an element to improve query efficiency.

What if I overwrite equals but not hashCode?

The Sudent class overrides only the equals method of Object, not the hashCode method \

Test it out:

Test results:

As long as the id and name of two Student objects are the same, we can assume that they are referring to the same person, so we override equals to make Student return true as long as the name and ID are the same, and we don’t override hashCode.

In the test, we create two new Student objects with the same ID and name. When we call equals, we return true, indicating that the two objects are equal. But because the hashCode method is not overridden, the hashCode that these two objects call is the same as the hashCode method in the Object class, and their values are not equal.

It follows that if you override equals but don’t override hashCode, you might end up with equals returning true and hashCode returning a different result.

So what’s the effect of that?

In Java’s underlying collection framework (such as HashMap, HashSet, etc.), in order to improve the efficiency of query, it is often necessary to call the object’s hashCode method to determine the storage location of an object.

For example, in the above example, we put the two newly created Student objects into the HashSet:

= true = true = true = true = true = true = true = true = true = true

The reason for this is that the underlying real-time array structure of a HashSet calls the object’s hashCode method when an object needs to be placed in it, processes the returned result, and finally calculates the object’s location in the HashSet.

Student does not override the hashCode method, so even if equals is true, they are saved at different locations in the underlying array, which does not trigger the HashSet deduplicate function, whereas for the programmer, two identical objects appear multiple times in the HashSet.