First, override the equals method
(1) add a Person class, test whether the name of the Person is the same, the result is false
(2) the light: because if you don’t rewrite the equals method, is called the Object by default equals method (pictured), compare the memory address, so the above two person Object equals the result is false(3) So override equals as follows(4) Test again, then the result is true
Second: Justify overriding the Hashcode method.
(1) Put two objects into a set and see the length of the set
(2) Test results are as follows(3) If the equals result is true for both persons, then there are two objects. (4) If the equals result is true for both persons, then there are two objects. Because set determines if the contents are duplicate,This is based on the hashcode methodObject’s hashcode() method generates a hashcode based on the memory address, but p1 and P2 are both new. So the hashcode is different, so there are still two objects in the set.
(4) Solution: rewrite the hashCode method(5) Test again, and the results are as follows