Why does the Equals () method need to be rewritten?
Determines whether an object is logically equal, and if the two strength is equal according to the class member variable, the Equals method in the inherited object can only determine whether two reference variables are the same object. So we often need to rewrite the Equals () method
When we add elements to a collection that does not have duplicates, the objects are often stored in the collection, and we need to determine whether the collection exists
How do I rewrite the Equals () method?
Overriding the requirements of the Equals method:
1, Reflexivity: For any non-null reference x,x.equals (x) should return true.
2, symmetry: for any reference to x and Y, if X.equals (y) returns True, then Y.equals (x) should also return true.
3, transitivity: For any reference x, Y, and Z, if X.equals (y) returns True,y.equals (Z) returns True, then X.equals (z) should also return true.
4. Consistency: If the objects referenced by x and y do not change, then repeated calls to X.equals (Y) should return the same result.
5, non-nullability: for any non-null reference x,x.equals (NULL) should return FALSE.
The list checks whether the element is contained by invoking the Equals method of the object, which means that contains (P2) passes
Java override Equals method