Java improvements (38) ----- Java Set details (4): Keep compareTo and equals synchronized
In Java, we often use the Comparable interface to implement sorting. compareTo is the method to implement this interface. We know that compareTo returns 0 to indicate that the two objects are equal, return positive number to indicate greater than, return negative number to indicate less. At the same time, we also know that equals can also determine whether two objects are equal, so are there associations between them? Copy the public class Student implements Comparable <Student> {private String id; private String name; private int age; public Student (String id, String name, int age) {this. id = id; this. name = name; this. age = age;} public boolean equals (Object obj) {if (obj = null) {return false;} if (this = obj) {return true ;} if (obj. getClass ()! = This. getClass () {return false;} Student student = (Student) obj; if (! Student. getName (). equals (getName () {return false;} return true;} public int compareTo (Student student) {return this. age-student. age;}/** the getter and setter methods are omitted */}. Copy the code Student class to implement the Comparable interface and the equals method. compareTo is compared based on age, equals is compared by name. Copy the public static void main (String [] args) {List <Student> list = new ArrayList <> (); list. add (new Student ("1", "chenssy1", 24); list. add (new Student ("2", "chenssy1", 26); Collections. sort (list); // sort Student student = new Student ("2", "chenssy1", 26); // retrieve the position of student in list int index1 = list. indexOf (student); int index2 = Collections. binarySearch (list, student); System. out. println ("index1 =" + ind Ex1); System. out. println ("index2 =" + index2);} copy the Code. According to the general idea, the two indexes should be consistent, because they retrieve the same object, but unfortunately, the running result is: index1 = 0index2 = 1. Why is this different result? This is because the implementation mechanism of indexOf and binarySearch is different. indexOf is implemented based on equals. As long as equals returns TRUE, it is considered that the same element has been found. BinarySearch is based on the compareTo method. When compareTo returns 0, it is deemed that this element has been found. In our implemented Student class, we override the compareTo and equals methods. However, our compareTo and equals methods are based on different comparisons. One is based on age and the other is based on name. The comparison results may vary depending on the differences. So when we know the reason, we just need to modify it: Make sure that the comparison between the two is consistent. For the compareTo and equals methods, we can conclude that compareTo is used to determine whether the elements are in the same position in the sorting, and equals is used to determine whether the elements are equal. Since one determines the sorting position and the other determines the same, therefore, it is very necessary to ensure that the equals of the sorting position should be equal when the sorting position is the same.