java中Comparator 和 Comparable的区别

1、Comparable的代码如下:

public interface Comparable<T> {
    public int compareTo(T o);
}

2、Comparator的代码如下

public interface Comparator<T> {
    int compare(T o1, T o2);
    boolean equals(Object obj);
    
    // jdk1.8 后的方法
    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));
    }
}

Comparable和Comparator的主要区别在于:

  (1).Comparator 和 Comparable都是Java中的内部比较器接口,都是用来实现对一个自定义的类进行排序

  (2). 不同的是实现Comparable接口是定义在类的内部,比较代码需要嵌入类的内部结构中

  (3).  Comparator 实现在类的外部,单独实现第一个比较器,不需要对原来的类进行结构上的变化,属于无侵入式的。

 

例如实现Comparable接口:

 

    class Score implements Comparable<Score>{  
      public int score;  
      public int time;  
        
        public int getScore() {  
        return score;  
    }  
      
    public void setScore(int score) {  
        this.score = score;  
    }  
      
    public int getTime() {  
        return time;  
    }  
      
    public void setTime(int time) {  
        this.time = time;  
    }  
      
        @Override  
        public int compareTo(Score o) {  
            if(this.time>o.time) return 1;  
            else if(this.time==o.time) return 0;  
            else return -1;  
        }  
      
        public Score(int score, int time) {  
            super();  
            this.score = score;  
            this.time = time;  
        }  
    }   

 

实现Comparator接口的例子:

    class ScoreComparator implements Comparator<Score>{  
      
        @Override  
        public int compare(Score o1, Score o2) {  
            if(o1.time>o2.time) return 1;  
            else if(o1.time==o2.time) return 0;  
            else return -1;  
        }     
    }  

然后调用ScoreComparator :

Arrays.sort(score, new ScoreComparator());

参考文献:http://blog.csdn.net/hll174/article/details/50996199

 

 

       

 

posted @ 2017-07-03 17:39  LiuHheng0315  阅读(2009)  评论(0编辑  收藏  举报