The Comparator interface java Detailed

Earlier we talked about for Java provides a comparable interface Comparable, provides a way of comparison, all the class that implements the interface, all the realization of the dynamic comparative method. In fact, in addition to comparing Java interface, but also it provides an interface that also has comparable functionality, but the focus of the interface is relatively vessel, which is then sorted, this is the Comparator, here we come to know about the specific ;

First, look at the source code:

package java.util;

import java.io.Serializable;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.function.ToDoubleFunction;
import java.util.Comparators;

@FunctionalInterface
public interface Comparator<T> {

    int compare(T o1, T o2);

    boolean equals(Object obj);

 
    default Comparator<T> reversed() {
        return Collections.reverseOrder(this);
    }

    default Comparator<T> thenComparing(Comparator<? super T> other) {
        Objects.requireNonNull(other);
        return (Comparator<T> & Serializable) (c1, c2) -> {
            int res = compare(c1, c2);
            return (res != 0) ? res : other.compare(c1, c2);
        };
    }

    default <U> Comparator<T> thenComparing(
            Function<? super T, ? extends U> keyExtractor,
            Comparator<? super U> keyComparator)
    {
        return thenComparing(comparing(keyExtractor, keyComparator));
    }

    default <U extends Comparable<? super U>> Comparator<T> thenComparing(
            Function<? super T, ? extends U> keyExtractor)
    {
        return thenComparing(comparing(keyExtractor));
    }

    default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor) {
        return thenComparing(comparingInt(keyExtractor));
    }

    default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor) {
        return thenComparing(comparingLong(keyExtractor));
    }

    default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) {
        return thenComparing(comparingDouble(keyExtractor));
    }
    public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() {
        return Collections.reverseOrder();
    }

    @SuppressWarnings("unchecked")
    public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
        return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE;
    }

    public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) {
        return new Comparators.NullComparator<>(true, comparator);
    }

    public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) {
        return new Comparators.NullComparator<>(false, comparator);
    }

    public static <T, U> Comparator<T> comparing(
            Function<? super T, ? extends U> keyExtractor,
            Comparator<? super U> keyComparator)
    {
        Objects.requireNonNull(keyExtractor);
        Objects.requireNonNull(keyComparator);
        return (Comparator<T> & Serializable)
            (c1, c2) -> keyComparator.compare(keyExtractor.apply(c1),
                                              keyExtractor.apply(c2));
    }

    public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
            Function<? super T, ? extends U> keyExtractor)
    {
        Objects.requireNonNull(keyExtractor);
        return (Comparator<T> & Serializable)
            (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
    }


    public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) {
        Objects.requireNonNull(keyExtractor);
        return (Comparator<T> & Serializable)
            (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2));
    }

    public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor) {
        Objects.requireNonNull(keyExtractor);
        return (Comparator<T> & Serializable)
            (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2));
    }


    public static<T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) {
        Objects.requireNonNull(keyExtractor);
        return (Comparator<T> & Serializable)
            (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2));
    }
}

 

We designed the class, it may not take into account to make the class implement the Comparable interface, then you need to use an additional interface to a comparator Comparator.

We can see from the previous example, compareTo (T o) is only one parameter, Comparator interface must be implemented compare (T o1, T o2) there are two parameters.

E.g:

class StudentComparator implements Comparator<Student>{  
  
    @Override  
    public int compare(Student o1, Student o2) {  
        // TODO Auto-generated method stub  
        if(o1.getScore()>o2.getScore())  
            return -1;  
        else if(o1.getScore()<o2.getScore())  
            return 1;  
        else{  
            if(o1.getAge()>o2.getAge())  
                return 1;  
            else if(o1.getAge()<o2.getAge())  
                return -1;  
            else   
                return 0;  
        }  
    }  
      
}  
  
  
public class ComparableDemo02 {  
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
  
        Student stu[]={new Student("zhangsan",20,90.0f),  
                new Student("lisi",22,90.0f),  
                new Student("wangwu",20,99.0f),  
                new Student("sunliu",22,100.0f)};  
        java.util.Arrays.sort(stu,new StudentComparator());  
        for(Student s:stu)  
        {  
            System.out.println(s);  
        }  
    } 

The above array is still student sort objects, are used Array.sort method, except that when implement a comparator, sort methods require parameters passed in two, i.e. stu array of objects, and rewriting achieved comparator comparison method class.

We are here simply to understand the Comparator methods

Guess you like

Origin blog.csdn.net/qq_36850813/article/details/94388667