1. What are equals and hashCode?

Equals compares the address values of two objects, both of which are objects

HashCode is compared by converting memory addresses to Int values via native methods

2. Use the context: The equals method compares the contents of two objects, and the HashCode method can only be used in collections

3. What is the hashCode rewrite rule?

If two objects are equal, hashCode must be equal

Two objects are not equal, hashCode is not necessarily equal

HashCode is equal. Two objects are not necessarily equal

HashCode is not equal, two objects must not be equal

4. What happens if you just override equals?

Equals is inefficient if you need to compare a large number of objects quickly. Hashset, hashmap, hashtable, etc. A hashset requires that objects must not be duplicated, and each object must be compared internally, so according to the comparison rules, if the hashcodes are the same, equals is used to verify, and if the Hashcodes are different, it is different, which improves efficiency

5. What if you just overwrite hashCode?

Overwriting hashCode only is not reliable, and sometimes the hashCode generated by two objects may be the same, and special cases may arise. Two objects equal to hashCode() must have equal hashCode(), which is absolutely reliable. Two objects equal to hashCode() must not have equal hashCode()

6. Why override hashCode when overriding equals?

In some Java containers, two objects are not allowed to be identical and will be overwritten if they are judged to be identical. If you override only equals () but not hashcode, the hashcode of Object is a hash converted from the storage address of the Object. In this case, it is possible to overwrite the same object by not overwriting the hashCode method, causing the same object to be hashed to a different location.

For example, overwriting a hashtable is based on the hash value and the equals of the key.