Both Comparable and Comparator are interfaces for comparison and sorting. What is the difference between them?
1, Comparable usage
When sorting Collections, we need to use a tool class called Collections, which can be used to sort Collections. See the following code for details:
List<Integer> list = new ArrayList<>(); list.add(14); list.add(30); list.add(3); list.add(12); Collections.sort(list); Sstem.out.println(list);
The printing result is: [3,12,14,30];
Obviously, the default sorting result of Collections for arrays of Integer type is ascending
So if we create a custom type Person array, can we sort it? You can try it with the code. The result is No. why is there such a problem? We can find the problem by looking at the sort method in Collections:
public static <T extends Comparable<? super T>> void sort(List<T> list) { list.sort(null); }
In the generic rule, there is a generic wildcard of T extends Comparable, which restricts the T in the list to be sorted, and requires that the T in the collection must implement the Comparable interface. We can write a Person class to implement the Comparable interface according to this idea, and there is an abstract method in this interface that we need to implement, This method is CompareTo
public class Person implements Comparable<Person>{ String name; Integer age; public Person(String name, int age) { super(); this.name = name; this.age = age; } public Person() { super(); } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } //Sorting rules public int compareTo(Person o) { //Reference types (types that can be sorted) can call the CompareTo method directly //Basic type -- use minus //return this.age - o.age;// Use this object - the objects in the parameter are arranged in ascending order of the attribute //return o.age - this.age; //return this.name.compareTo(o.name); //return o.name.compareTo(this.name); return this.age.compareTo(o.age); }
The compareTo method is actually the sorting rules we need to set, and how to sort. Simply remember that using this object and parameter comparison is ascending, and vice versa. So if we want to arrange the objects in the Person collection in descending order by age, we can use o.age - this ageļ¼ (basic types can use subtraction instead of compareTo);
In this way, you can use collections again Sort can sort the List of persons, and the sorting result is in descending order by age.
To sum up, if we want to make a List, we can use collections Sort (List) method, the element types in the collection must be required to implement the Comparable interface, that is, to enable them to compare. This is why arrays of Integer type can be sorted, because Integer has implemented the interface and it is implemented according to the ascending rule, This explains why the result of the first program above is in ascending order. Well, since integers are sorted in ascending order, what should I do if I want to get a set of integers in descending order? Can't it be realized? Let's move on to the next interface.
2, Comparator
As mentioned above, for collections that have implemented the Comparable interface, or I don't want to implement the Comparable interface at all, can't we arrange the order, or can't we change the sorting rules? In fact, it's not. We can sort in another way, that is, using the Comparator interface.
There is another method in the tool class of the collection: public static < T > void sort (list < T > list, comparator <? Super T > C)
We can realize the above requirements through this method:
Collections.sort(list,new Comparator<Integer>(){ @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } });
For example, this code implements a descending arrangement of Integer sets. There is a method called compare in this interface, which contains two parameters: if you compare the first one with the second one, you get ascending order, otherwise you get descending order. Similarly, you can use this method to sort the memory of our own defined classes.
OK, this is the usage of the Comparable interface and Comparator interface. In addition, note:
The Comparable interface is located in Java Lang package, the Comparator interface is located in Java Util package.
Comparable: internal comparator, a class if you want to use collections Sort (list) method, you need to implement this interface
Comparator: the external comparator is used to sort those that do not implement the Comparable interface or are not satisfied with the sorting rules in the implemented Comparable It is more flexible without changing the structure of the class. (policy mode)