preface
Comparable and comparator correspond to what we call natural sorting and comparator sorting respectively. Comparator is more flexible than Comparable. A class implements the Comparable interface and implements the compareTo(T o) method (the return value of this method is int). If the return value is positive, it means that the current object (the object calling this method) is "larger" than the obj object; On the contrary, "small"; If it is zero, it means that two objects are equal) can define a comparison method, but sometimes it is necessary to sort the same object in many different ways. At this time, should we change the implementation code in compare (T o)? The answer is No. in order to make up for this deficiency, JDK also provides us with another sorting method, that is, comparator sorting.
1, Introduction to Comparable
if a class implements the comparable interface, it means that the class supports sorting. Comparable is an interface with only one method.
package java.lang; import java.util.*; public interface Comparable<T> { public int compareTo(T o); }
The list or array of objects of classes that implement the Comparable interface can be accessed through collections Sort or arrays Sort to sort automatically.
Code example:
Student class:
public class Student implements Comparable<Student>{ private String name; private int age; private int tall;//height Student(String name,int age,int tall){ this.name = name; this.age = age; this.tall = tall; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getTall() { return tall; } public void setTall(int tall) { this.tall = tall; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", tall='" + tall + '\'' + '}'; } @Override public int compareTo(Student o) { if(o.age > age){ return -1; }else if(o.age < age){ return 1; } return 0; } }
Test code:
public class Test { public static void main(String[] args) { Student[] students = new Student[10]; for(int i=0;i<10;++i){ students[i] = new Student(Integer.toString(i),new Random().nextInt(20),new Random().nextInt(160)); } //Arrays.sort(students); //Collections.sort(Arrays.asList(students)); for(Student stu : students){ System.out.println(stu); } } }
2, Introduction to Comparator
Comparator is a comparison interface. If we need to control the order of a class and the class itself does not support sorting (that is, it does not implement the Comparable interface), we can establish a "Comparator of this class" to sort. This "Comparator" only needs to implement the Comparator interface. In other words, we can create a new Comparator by implementing the Comparator interface and the compare(T o1, T o2) method, and then sort the classes through this Comparator.
Now there is a need for us to sort the students according to their height, but what if we don't change the code above? At this time, Comparator's advantages are brought into play.
The code is as follows:
public class Test{ public static void main(String[] args) { Student[] students = new Student[10]; for(int i=0;i<10;++i){ students[i] = new Student(Integer.toString(i),new Random().nextInt(20),new Random().nextInt(160)); } myComparator mycomparator = new myComparator(); //Arrays.sort(students,mycomparator); //Collections.sort(Arrays.asList(students),mycomparator); for(Student stu : students){ System.out.println(stu); } } } class myComparator implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o1.getTall() > o2.getTall() ? 1 : o1.getTall() == o2.getTall() ? 0 : -1; } }
summary
Compared with the two:
1. Using Comparable is relatively simple. As long as the object implementing the Comparable interface directly becomes a Comparable object, but the source code of the class needs to be modified, which is more destructive to the class.
2. The advantage of using Comparator is that there is no need to modify the source code, but to implement another Comparator. When a user-defined object needs to be compared, the size can be compared by passing the Comparator and the object together. In Comparator, users can implement complex and universal logic to match some simple objects, Compared with Comparable, it is more flexible and decoupled.