For the comparator interface and comparable interface, and their respective methods, compare (), compareTo (), and compareto
Two of the LeetCode questions today show the use of interfaces to sort objects. Links to the two questions are as follows:
1. Sort objects using the comparable Interface
2. sort by using the comparator Interface
Since I have never touched these two interfaces before and cannot understand their functions at the moment, I have searched for a lot of information on the Internet. Now I have roughly figured out some information. Now I have recorded what is missing, thank you for your prompt correction.
1. Comparable <T> Interface
In the java API documentation, this interface is used to forcibly sort the objects of every class that implements it-called the natural sorting of this class. The objects and arrays of this interface can be Collections. sort (), and Arrays. sort () for Automatic Sorting;
That is to say, as long as the object (array) implementing this interface has the ability to sort, it is called comparable --- sortable, so it can be said that this isInternal sortingBy implementing its unique method compareTo (), this method will be mentioned below;
2. Comparator <T> Interface
For it, it is for some objects (arrays) that are not capable of comparison for them to achieve the function of comparison, so it is calledComparatorIsExternalSomething through itDefine the comparison method, And then upload it to Collection. sort () and Arrays. sort () to sort the objects, and define the comparison content and the ascending/descending order of the results through its own method compare;
3. Questions about descending order of sorting results
When learning to use these two interfaces, we can see that every program implementing them is doing this.
For compareTo ():
Public int compareTo (Object o) {return this. val-o.val; // assume that the val attribute is compared}
In this way, the objects implementing the comparable interface can be sorted in ascending order;
The compare () method is as follows:
Ascending: public int compare (Object a, Object B) {return a. val-b.val;} descending: public int compare (Object a, Object B) {return B. val-a.val ;}
When I saw the implementation of these two methods, I felt a little confused. I went to the API documentation, but I only mentioned that 1,-, and 0 were returned based on the size; why can different results be obtained after this method is uploaded to the sort () function? Because the sizes of the two objects are inconsistent, the returned values are not necessarily the same. Therefore, I don't think the values will be determined in ascending or descending order based on the positive and negative values of the returned results, so I have read a lot of answers online, and I am not very clear about them. I just mentioned how we can sort them in ascending order and how we can sort them in descending order, but I have not explained why and how we can judge them?
Later I foundIn Arrays, the sort () function can sort all or some elements in the array in ascending order !!!Therefore, the sort () function called after these two interfaces are sorted in ascending order. Therefore,The above two methods exist to determine the size of the two elements. sort () will sort all the elements in ascending order.;
For example, compareTo () is an internal sorting Implementation of objects. It is arranged according to the natural order of objects. When determining the size of two objects, it will refer to the results of compareTo, if the value is greater than 0, this. val is large, and vice versa. Similarly, when the comparetor comparator is called, sort () is passed into the compare (a, B) parameters, and the return value is greater than 0 by default, that is, the value of a is greater than B, and vice versa; therefore, if we judge the order of two objects by sort () to implement the compareTo () and compare () methods, the results will be in ascending order. If we reverse the process, we use o. val-this.val or B. val-a.val then the obtained value is the opposite of the correct two values, and sort () does not know according to their own thought in ascending order to determine the order, small before, big after; in this case, the result is sorted in descending order ~~~~