1. 程式人生 > >Comparable與Comparator源碼分析

Comparable與Comparator源碼分析

doc 同時 inf 兩個 span ase err 需要 ren

package java.lang;
import java.util.*;

/**
 * This interface imposes a total ordering on the objects of each class that
 * implements it.  This ordering is referred to as the class‘s <i>natural
 * ordering</i>, and the class‘s <tt>compareTo</tt> method is referred to as
 * its <i>natural comparison method</i>.<p>
 *
 * Lists (and arrays) of objects that implement this interface can be sorted
 * automatically by {
@link Collections#sort(List) Collections.sort} (and * {@link Arrays#sort(Object[]) Arrays.sort}). Objects that implement this * interface can be used as keys in a {@linkplain SortedMap sorted map} or as * elements in a {@linkplain SortedSet sorted set}, without the need to * specify a {@linkplain
Comparator comparator}.<p> * * The natural ordering for a class <tt>C</tt> is said to be <i>consistent * with equals</i> if and only if <tt>e1.compareTo(e2) == 0</tt> has * the same boolean value as <tt>e1.equals(e2)</tt> for every * <tt>e1</tt> and <tt>e2</tt> of class <tt>C</tt>. Note that <tt>null</tt> * is not an instance of any class, and <tt>e.compareTo(null)</tt> should * throw a <tt>NullPointerException</tt> even though <tt>e.equals(null)</tt> * returns <tt>false</tt>.<p> * * It is strongly recommended (though not required) that natural orderings be * consistent with equals. This is so because sorted sets (and sorted maps) * without explicit comparators behave "strangely" when they are used with * elements (or keys) whose natural ordering is inconsistent with equals. In * particular, such a sorted set (or sorted map) violates the general contract * for set (or map), which is defined in terms of the <tt>equals</tt> * method.<p> * * For example, if one adds two keys <tt>a</tt> and <tt>b</tt> such that * {
@code (!a.equals(b) && a.compareTo(b) == 0)} to a sorted * set that does not use an explicit comparator, the second <tt>add</tt> * operation returns false (and the size of the sorted set does not increase) * because <tt>a</tt> and <tt>b</tt> are equivalent from the sorted set‘s * perspective.<p> * * Virtually all Java core classes that implement <tt>Comparable</tt> have natural * orderings that are consistent with equals. One exception is * <tt>java.math.BigDecimal</tt>, whose natural ordering equates * <tt>BigDecimal</tt> objects with equal values and different precisions * (such as 4.0 and 4.00).<p> * * For the mathematically inclined, the <i>relation</i> that defines * the natural ordering on a given class C is:<pre> * {(x, y) such that x.compareTo(y) &lt;= 0}. * </pre> The <i>quotient</i> for this total order is: <pre> * {(x, y) such that x.compareTo(y) == 0}. * </pre> * * It follows immediately from the contract for <tt>compareTo</tt> that the * quotient is an <i>equivalence relation</i> on <tt>C</tt>, and that the * natural ordering is a <i>total order</i> on <tt>C</tt>. When we say that a * class‘s natural ordering is <i>consistent with equals</i>, we mean that the * quotient for the natural ordering is the equivalence relation defined by * the class‘s {@link Object#equals(Object) equals(Object)} method:<pre> * {(x, y) such that x.equals(y)}. </pre><p> * * This interface is a member of the * <a href="{@docRoot}/../technotes/guides/collections/index.html"> * Java Collections Framework</a>. * * @param <T> the type of objects that this object may be compared to * * @author Josh Bloch * @see java.util.Comparator * @since 1.2 */ public interface Comparable<T> { public int compareTo(T o); }

實現這個接口的類的集合或數組將會被自動排序通過Collections.sort或者Arrays.sort。同時,這個對象也可以被用來做Map或者Set的鍵值,而不需要另外制定一個比較器。

廢話不多說了,直接舉例子。

package object;
public class Person implements Comparable<Object>{
    private String name;
    private int age;
    Person(String name,int age){
        this.name=name;
        this.age=age;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public String toString() {
        return "姓名:"+this.name +";年齡: " + this.age;
    }
    @Override
    public int compareTo(Object o) {
        Person temp =null;
        if(o instanceof Person )
            temp = (Person)(o);
        return this.age - temp.age;
    }
}

測試類:

public class Test{
    @org.junit.Test
    public void test(){
        Person[] people = {new Person("tom",21),new Person("jerry",18),new Person("dog",17)};
        System.out.println("排序前");
        for(Person p:people)
            System.out.println(p);
        System.out.println("排序後");
        java.util.Arrays.sort(people);
        for(Person p:people)
            System.out.println(p);
    }
}

結果:

技術分享圖片

大家可以看到,這個排序方法是放到排序的對象類裏面的,但是如果我們已經設計好了某類,而且不想改變其數據結構,就這可以用到比較器Comparator。用 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<T> {

    int compare(T o1, T o2);
    boolean equals(Object obj);

}

  Comparator裏面只有兩個無實現體的接口方法,其它的方法有實現體,好像是新特性,暫時未用到。這裏的equals與Object的equals方法類似。所以對於實現這個接口的類不必實現equals方法,也不會報錯。

下面的例子我用的是匿名函數。

package object;
public class Person{
    private String name;
    private int age;
    Person(String name,int age){
        this.name=name;
        this.age=age;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public String toString() {
        return "姓名:"+this.name +";年齡: " + this.age;
    }
}

測試類:

public class Test{
    @org.junit.Test
    public void test(){
        Person[] people = {new Person("tom",21),new Person("jerry",18),new Person("dog",17)};
        System.out.println("排序前");
        for(Person p:people)
            System.out.println(p);
        System.out.println("排序後");
        Arrays.sort(people, new Comparator<Person>() {
            public int compare(Person o1, Person o2) {
                return o1.getAge()-o2.getAge();};
        });
        for(Person p:people)
            System.out.println(p);
    }
}

結果:

技術分享圖片

總結:

  用Comparable 簡單, 只要實現Comparable 接口的對象直接就成為一個可以比較的對象,但是需要修改源代碼,用Comparator 的好處是不需要修改源代碼,而是另外實現一個比較器,當某個自定義的對象需要作比較的時候,把比較器和對象一起傳遞過去就可以比大小了,並且在Comparator 裏面用戶可以自己實現復雜的可以通用的邏輯,使其可以匹配一些比較簡單的對象,那樣就可以節省很多重復勞動了。

Comparable與Comparator源碼分析