Java override Equals method

Source: Internet
Author: User

Override equals: compare content.

Hashcode: Content of Hashcode ().


/*

* Overriding equals must be noted:
* 1 reflexivity: For any reference value x,x.equals (x) must be True
* 2 symmetry: for arbitrary reference values x and Y, when X.equals (y) returns True,y.equals (x) also must return True
* 3 transitivity: For arbitrary reference values x, Y, and Z, if X.equals (y) returns True, and Y.equals (Z) also returns True, then X.equals (z) must also return true
* 4 Consistency: For arbitrary reference values x and Y, if the object information used for the equals comparison has not been modified,
* Multiple calls to X.equals (Y) either return true uniformly or return false consistently
* 5 non-nullability: for any non-null reference value x,x.equals (NULL) must return FALSE
*
* Please note:
* It is best to override the Hashcode method after overriding the Equals method, otherwise two equivalent objects may get different hashcode, which can have serious consequences when used in a collection frame
*/


/*
* 1. Overriding the Equals method modifier must be public because it is a method of overriding object.
* 2. The parameter type must be object.
*/
public boolean equals (Object) {//Overrides the Equals method, it is better to override the Hashcode method later

if (this = other)//first check whether it is reflexive, then compare other if it is empty. This is high efficiency.
return true;
if (other = = null)
return false;
if (! ( Other instanceof Cat))
return false;

Final Cat cat = (cat) Other;

if (!getname (). Equals (Cat.getname ()))
return false;
if (!getbirthday (). Equals (Cat.getbirthday ()))
return false;
return true;
}

The public int hashcode () {//hashcode is mainly used to improve the query efficiency of the hash system. When nothing is done in hashcode, you can simply return a constant or do not rewrite it.
int result = GetName (). Hashcode ();
result = * Result +getbirthday (). Hashcode ();
return result;
return 0;
}

http://blog.csdn.net/min123456520/article/details/5194030

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.