Comparable and Comparator compare implement sorting scene analysis

 

Source code analysis - Collections.sort ()

       A, Collection.sort use

        Collections.sort (): the method is a set of positive sort of first set of element types of incoming Collections.sort () to inherit Comparator <T>, so as to ensure you can compare and sort.

       

 

 

     According to source code analysis, there are two main ways to achieve:

     (1) Sorting of conventional type and other types String

     (2) for a JavaBean properties (such as time, price, age sort) to sort

   Use Cases:

 List<String> listOne = Arrays.asList("3", "2", "5", "8");
        The Collections.sort (in listOne); // default ascending 
        System.out.println ( "in listOne =" + in listOne);

        /**
         * Output:
         * listOne = [2, 3, 5, 8]
         */

Two, Collections.sort of javabean sort of list

   The underlying algorithm is actually converted into a set of Array, then execute arrays.sort, arrays.sort use merge sort, quick drain optimization, timSort like.

   2, the data sort string type

    String analysis of source code;

      

 

 

    Because String type implements Comparable <String> interfaces, can be used Collections.sort (unSorted) sorting;

  List<String> unSorted = new ArrayList<String>();
  Collections.sort (UNSORTED); // String implements Comparable interface

  Note: Due to custom javabean not implemented, it is not directly used, but a comparator may be implemented:

method one:

Collections.sort(unSorted,new Comparator<unSortedBean>() {
            @Override
            public int compare(unSortedBean arg0, unSortedBean arg1) {
                // 升叙
                return arg0.getOrder()-arg1.getOrder();
            }
        });
        

Second way :

     Can also be used Collections.sort (); mode, only javabean implement the interface and to achieve comparable ascending or descending order by the compareTo () method;

import java.io.Serializable;

public class unSortedBean implements Serializable, Comparable<unSortedBean>{

    private static final long serialVersionUID = 1L;
    
    private String name;
    private String age;
    private int order;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    
    public int getOrder() {
        return order;
    }
    public void setOrder(int order) {
        this.order = order;
    }
    @Override
    public int compareTo(unSortedBean arg0) {
        // TODO 升叙
        return this.order-arg0.getOrder();
    }
    

}
Note: If the order is string type numbers need to be converted into digital type and then compare, or comparison results may not be as expected.

Three, the Comparable and Comparator difference

1, for Comparable interface, the class is the need for direct comparison target belongs implement Comparable interface, the interface implementation class is given a natural order, and that only a natural order, and a comparator Comparator interface, the comparison target belongs the class does not directly implement the interface, the comparator can write a class that implements this interface alone, as a comparison target of the comparators, for a class, a plurality of comparators may be implemented.

2, Comparator can choose to compare to null , and Comparable not. Mainly because Comparator is a parameter of comparison is compare methods and comparison methods compareTo Comparable methods need to call the object, while the object is null (null.compareTo (obj)), will be abnormal.
 

Key: Comparable <T> and Comparator <T> test case: a reference link address

 

Guess you like

Origin www.cnblogs.com/2019wxw/p/12217316.html