Java override Equals method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.