The reason for overriding equals is simple: when not overridden, equals compares addresses for reference data types, i.e

Person A = new Person();
Person B = new Person();

A.setId(1);
B.setId(1);
Copy the code

A.equals(B) returns false, but logically we should assume that these two are the same person, so we need to override equals to satisfy this requirement.

Why rewrite hashCode? Because hashCode is related to addresses, two objects with the same logic have different hashcodes.

If you put it in a HashSet, check the hashCode first and find that it does not conflict, and both logically identical objects will be put in the HashSet