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