java的Comparator接口详解

前面我们讲过Java提供了一个用于比较的接口Comparable,提供了一个比较的方法,所有实现该接口的类,都动态的实现了该比较方法。实际上Java中除了比较一个接口外,还提供了一个接口,该接口也是具有比较的功能,但该接口注重的却是比较容器,然后对其排序,这就是Comparator,下面我们就来具体认识一下;

首先看一下源码:

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 {

    int compare(T o1, T o2);

    boolean equals(Object obj);

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

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

    default  Comparator thenComparing(
            Function keyExtractor,
            Comparator keyComparator)
    {
        return thenComparing(comparing(keyExtractor, keyComparator));
    }

    default > Comparator thenComparing(
            Function keyExtractor)
    {
        return thenComparing(comparing(keyExtractor));
    }

    default Comparator thenComparingInt(ToIntFunction keyExtractor) {
        return thenComparing(comparingInt(keyExtractor));
    }

    default Comparator thenComparingLong(ToLongFunction keyExtractor) {
        return thenComparing(comparingLong(keyExtractor));
    }

    default Comparator thenComparingDouble(ToDoubleFunction keyExtractor) {
        return thenComparing(comparingDouble(keyExtractor));
    }
    public static > Comparator reverseOrder() {
        return Collections.reverseOrder();
    }

    @SuppressWarnings("unchecked")
    public static > Comparator naturalOrder() {
        return (Comparator) Comparators.NaturalOrderComparator.INSTANCE;
    }

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

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

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

    public static > Comparator comparing(
            Function keyExtractor)
    {
        Objects.requireNonNull(keyExtractor);
        return (Comparator & Serializable)
            (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
    }


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

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


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

 

我们设计类的时候,可能没有考虑到让类实现Comparable接口,那么就需要用到另外的一个比较器接口Comparator。

从之前的实例我们可以发现,compareTo(T o)只有一个参数,而Comparator接口中必须要实现的compare(T o1,T o2)就有两个参数。

例如:

class StudentComparator implements Comparator{  
  
    @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.getAge())  
                return 1;  
            else if(o1.getAge()

上面依然是对student对象数组进行排序,用的都是Array.sort方法,不同的是实现comparator接口时,sort方法需要传进来两个参数,即stu对象数组,以及重写的实现了comparator比较方法类。

我们这里只是简单了解了Comparator的方法

你可能感兴趣的:(java)