From: http://blog.csdn.net/afgasdg/article/details/6889383 #
------------------------------------------------------
I. Functions of the equals Method
1. By default (The equals method is not overwritten), The equals method calls the equals method of the object class, the equals method of the object is mainly used to determine whether the object's memory address reference is the same address (whether it is the same object ).
2. If the equals method is overwritten in the class, the function of the equals method should be determined based on the specific code. After overwriting, the object content is usually used to determine whether the object is equal.
The code for not overwriting the equals method is as follows:
// Student class public class student {private int age; private string name; Public student () {} public student (INT age, string name) {super (); this. age = age; this. name = Name;} public int getage () {return age;} Public String getname () {return name;} public void setage (INT age) {This. age = age;} public void setname (string name) {This. name = Name ;}}
The test code is as follows:
Import Java. util. hashset; import Java. util. using list; import Java. util. set; public class synchronized stest {public static void main (string [] ARGs) {comment list <student> List = new comment list <student> (); set <student> set = new hashset <student> (); Student stu1 = new student (3, "Zhang San"); Student stu2 = new student (3, "Zhang San "); system. out. println ("stu1 = stu2:" + (stu1 = stu2); system. out. println ("stu1.equals (stu2):" + stu1.equals (stu2); list. add (stu1); list. add (stu2); system. out. println ("list size:" + list. size (); set. add (stu1); set. add (stu2); system. out. println ("set size:" + set. size ());}}
Running result:
Stu1 = stu2: false
Stu1.equals (stu2): false
List size: 2
Set size: 2
Result Analysis: the student class does not overwrite the equals method. stu1 calls the equals method and actually calls the equals method of the object. Therefore, whether the object memory address is equal is used to determine whether the object is equal. Because it is two new objects, the object's memory address is not equal, so stu1.equals (stu2) is false.
3. overwrite the equals method (age and name attributes), and let the student class determine whether the object is equal by determining whether the object content is equal. Covered student class:
// Student class public class student {private int age; private string name; Public student () {} public student (INT age, string name) {super (); this. age = age; this. name = Name;} public int getage () {return age;} Public String getname () {return name;} public void setage (INT age) {This. age = age;} public void setname (string name) {This. name = Name ;}@ overridepublic Boolean equals (Object OBJ) {If (this = OBJ) return true; If (OBJ = NULL) return false; If (getclass ()! = Obj. getclass () return false; Student Other = (student) OBJ; If (age! = Other. Age) return false; If (name = NULL) {If (other. Name! = NULL) return false;} else if (! Name. Equals (other. Name) return false; return true ;}}
Running result:
Stu1 = stu2: false
Stu1.equals (stu2): True
List size: 2
Set size: 2
Result Analysis: the age and name attributes of the student objects are equal, and are determined by overwriting the equals method. stu1.equals (stu2) is true. Note that the size of the list and set tests are both 2.
Ii. hashcode
4. Run the above code and we know that the equals method has taken effect. Next, we will overwrite the hashcode method (generate hashcode through the age and name attributes) and not overwrite the equals method. The hash code is generated by age and name.
Overwrite the student class after hashcode:
[Java]View plaincopy
// Student class public class student {private int age; private string name; Public student () {} public student (INT age, string name) {super (); this. age = age; this. name = Name;} public int getage () {return age;} Public String getname () {return name;} public void setage (INT age) {This. age = age;} public void setname (string name) {This. name = Name ;}@ overridepublic int hashcode () {final int prime = 31; int result = 1; resu Lt = prime * result + age; Result = prime * result + (name = NULL )? 0: Name. hashcode (); return result ;}}
Running result:
Stu1 = stu2: false
Stu1.equals (stu2): false
List size: 2
Hashcode: 775943
Hashcode: 775943
Set size: 2
Result Analysis: The equals method is not overwritten and only the hashcode method is overwritten. Although the two objects have the same hashcode, however, when stu1 and stu2 are put into the set, the hashcode value of the two objects is not compared because the two objects compared by the equals method are false.
5. overwrite the equals method and hashcode method. The student code is as follows:
[Java]View plaincopyprint?
// Student class public class student {private int age; private string name; Public student () {} public student (INT age, string name) {super (); this. age = age; this. name = Name;} public int getage () {return age;} Public String getname () {return name;} public void setage (INT age) {This. age = age;} public void setname (string name) {This. name = Name ;}@ overridepublic int hashcode () {final int prime = 31; int result = 1; resu Lt = prime * result + age; Result = prime * result + (name = NULL )? 0: Name. hashcode (); system. out. println ("hashcode:" + result); return result ;}@ overridepublic Boolean equals (Object OBJ) {If (this = OBJ) return true; If (OBJ = NULL) return false; If (getclass ()! = Obj. getclass () return false; Student Other = (student) OBJ; If (age! = Other. Age) return false; If (name = NULL) {If (other. Name! = NULL) return false;} else if (! Name. Equals (other. Name) return false; return true ;}}
Running result:
stu1 == stu2 : false
Stu1.equals (stu2): True
List size: 2
Hashcode: 775943
Hashcode: 775943
Set size: 1
Result Analysis: stu1 and stu2 are equal through the equals method and return the same hashcode value. Therefore, only one object is put into the set.
6. Let's make the equals methods of the two objects equal, but try if the hashcode value is not equal.
The code of the student class is as follows:
[Java]View plaincopyprint?
// Student class public class student {private int age; private string name; Private Static int Index = 5; Public student () {} public student (INT age, string name) {super (); this. age = age; this. name = Name;} public int getage () {return age;} Public String getname () {return name;} public void setage (INT age) {This. age = age;} public void setname (string name) {This. name = Name ;}@ overridepublic int hashcode () {final int PRI Me = 31; int result = 1; Result = prime * result + (age + index ++); Result = prime * result + (name = NULL )? 0: Name. hashcode (); system. out. println ("Result:" + result); return result ;}@ overridepublic Boolean equals (Object OBJ) {If (this = OBJ) return true; If (OBJ = NULL) return false; If (getclass ()! = Obj. getclass () return false; Student Other = (student) OBJ; If (age! = Other. Age) return false; If (name = NULL) {If (other. Name! = NULL) return false;} else if (! Name. Equals (other. Name) return false; return true ;}}
Running result:
Stu1 = stu2: false
Stu1.equals (stu2): True
List size: 2
Hashcode: 776098
Hashcode: 776129
Set size: 2
Result Analysis: Although stu1 and stu2 are equal through the equals method, the hashcode values of the two objects are not equal. Therefore, when stu1 and stu2 are put into the set, they are considered to be two different objects.
7. modify a property value of stu1
The student code is as follows:
[Java]View plaincopyprint?
// Student class public class student {private int age; private string name; Public student () {} public student (INT age, string name) {super (); this. age = age; this. name = Name;} public int getage () {return age;} Public String getname () {return name;} public void setage (INT age) {This. age = age;} public void setname (string name) {This. name = Name ;}@ overridepublic int hashcode () {final int prime = 31; int result = 1; resu Lt = prime * result + age; Result = prime * result + (name = NULL )? 0: Name. hashcode (); system. out. println ("hashcode:" + result); return result ;}@ overridepublic Boolean equals (Object OBJ) {If (this = OBJ) return true; If (OBJ = NULL) return false; If (getclass ()! = Obj. getclass () return false; Student Other = (student) OBJ; If (age! = Other. Age) return false; If (name = NULL) {If (other. Name! = NULL) return false;} else if (! Name. Equals (other. Name) return false; return true ;}}
The test code is as follows:
[Java]View plaincopyprint?
Import Java. util. hashset; import Java. util. using list; import Java. util. set; public class synchronized stest {public static void main (string [] ARGs) {comment list <student> List = new comment list <student> (); set <student> set = new hashset <student> (); Student stu1 = new student (3, "Zhang San"); Student stu2 = new student (3, "Zhang San "); system. out. println ("stu1 = stu2:" + (stu1 = stu2); system. out. println ("stu1.equals (stu2):" + stu1.equals (stu2); list. add (stu1); list. add (stu2); system. out. println ("list size:" + list. size (); set. add (stu1); set. add (stu2); system. out. println ("set size:" + set. size (); stu1.setage (34); system. out. println ("Remove stu1:" + set. remove (stu1); system. out. println ("set size:" + set. size ());}}
Running result:
Stu1 = stu2: false
Stu1.equals (stu2): True
List size: 2
Hashcode: 775943
Hashcode: 775943
Set size: 1
Hashcode: 776904
Remove stu1: false
Set size: 1
Result Analysis:
When we save an object to the set, if the attribute of this object participates in the calculation of hashcode, then the attributes of this object involved in hashcode calculation cannot be modified in the future, otherwise, it may cause errors that are not intended. As in the test, stu1 objects cannot be removed.
Summary:
1. The equals method is used to compare whether the content of an object is equal (after overwriting)
2. The hashcode method is only used in the set.
3. When the equals method is overwritten, the comparison object will be compared through the overwritten equals method (to determine whether the object content is equal ).
4. When an object is put into a set, first determine whether the hashcode value of the object to be put is equal to the hashcode value of any element in the set. If not, directly add the object to the set. If the hashcode value is equal, and then the equals method is used to determine whether the object to be put is equal to any object in the set. If equals judges that it is not equal, the element is directly put into the set, otherwise, do not place it.
5. flowchart for placing elements in a set: