java對象對比之comparable和comparator的區別

一、元素的比較

1.1 基本類型的比較

java中的基本類型的對象是可以進行比較的

public static void main(String[] args){
        int a = 10;
        int b = 20;
        System.out.println(a>b);
        System.out.println(a==b);
        System.out.println(a<b);

        char c1 = 'A';
        char c2 = 'B';
        System.out.println(c1>c2);
        System.out.println(c1==c2);
        System.out.println(c1<c2);

        boolean b1 = true;
        boolean b2 =false;
        System.out.println(b1==b2);
        System.out.println(b1!=b2);
    }

在這裡插入圖片描述

1.2 對象的比較

public class Main{
    public static void main(String[] args){
       Card c1 = new Card(1,"♠");
        Card c2 = new Card(2,"♠");
        Card c3 = c1;
        System.out.println(c1==c2);
        System.out.println(c1==c3);
//        System.out.println(c1>c2);  編譯報錯
//        System.out.println(c1<c2);  編譯報錯
    }

}
class  Card{
    public int rank;
    public String suit;
     public Card(int rank,String suit){
         this.rank = rank;
         this.suit = suit;
     }
}

在這裡插入圖片描述

可以看出在進行相等比較時,是可以進行比較的,但進行大於或小於比較就不行瞭
這是因為對於用戶實現自定義類型,都默認繼承自Object類,而Object類中提供瞭equal方法,而==默認情況下調用的就是equal方法,但是該方法的比較規則是:沒有比較引用變量引用對象的內容,而是直接比較引用變量的地址,但有些情況下該種比較就不符合題意。

二、對象的比較

有些情況下,需要比較的是對象中的內容,比如:向優先級隊列中插入某個對象時,需要對按照對象中內容來調整堆,那該如何處理呢?

2.1 覆寫基類的equal

在這裡插入圖片描述

一般覆寫 equals 的套路就是上面演示的

1.如果指向同一個對象,返回 true

2.如果傳入的為 null,返回 false

3.如果傳入的對象類型不是 Card,返回 false

4.按照類的實現目標完成比較,例如這裡隻要花色和數值一樣,就認為是相同的牌

5.註意下調用其他引用類型的比較也需要 equals,例如這裡的 suit 的比較
覆寫基類equal的方式雖然可以比較,但缺陷是:equal隻能按照相等進行比較,不能按照大於、小於的方式進行比較。

2.2 基於Comparable接口的比較

Comparble是JDK提供的泛型的比較接口類,源碼實現具體如下:

public interface Comparable<T> {
    /**
     * Compares this object with the specified object for order.  Returns a
     * negative integer, zero, or a positive integer as this object is less
     * than, equal to, or greater than the specified object.
     *
     * <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==
     * -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
     * implies that <tt>x.compareTo(y)</tt> must throw an exception iff
     * <tt>y.compareTo(x)</tt> throws an exception.)
     *
     * <p>The implementor must also ensure that the relation is transitive:
     * <tt>(x.compareTo(y)&gt;0 &amp;&amp; y.compareTo(z)&gt;0)</tt> implies
     * <tt>x.compareTo(z)&gt;0</tt>.
     *
     * <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>
     * implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
     * all <tt>z</tt>.
     *
     * <p>It is strongly recommended, but <i>not</i> strictly required that
     * <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>.  Generally speaking, any
     * class that implements the <tt>Comparable</tt> interface and violates
     * this condition should clearly indicate this fact.  The recommended
     * language is "Note: this class has a natural ordering that is
     * inconsistent with equals."
     *
     * <p>In the foregoing description, the notation
     * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
     * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
     * <tt>0</tt>, or <tt>1</tt> according to whether the value of
     * <i>expression</i> is negative, zero or positive.
     *
     * @param   o the object to be compared.
     * @return  a negative integer, zero, or a positive integer as this object
     *          is less than, equal to, or greater than the specified object.
     *
     * @throws NullPointerException if the specified object is null
     * @throws ClassCastException if the specified object's type prevents it
     *         from being compared to this object.
     */
    public int compareTo(T o);
}

可以看到在Comparable接口中隻實現瞭一個方法 compareTo,因此我們在實現自定義比較時,在類的定義中實現Comparable接口即可,然後在類中重寫compareTo方法

public class Main{
    public static void main(String[] args){
        Card c1 = new Card(1,"♠");
        Card c2 = new Card(2,"♠");
        Card c3 = c1;
        System.out.println(c1.compareTo(c2));
        System.out.println(c1.compareTo(c3));
        System.out.println(c2.compareTo(c3));
        
    }

}
class  Card implements Comparable<Card>{
    public int rank;
    public String suit;
     public Card(int rank,String suit){
         this.rank = rank;
         this.suit = suit;
     }

    @Override
    public int compareTo(Card o) {
        if(o==null){
            return 1;
        }
        return rank-o.rank;
    }
}

在這裡插入圖片描述

當前值比要比較值小則輸出-1;當前值與要比較值相等則輸出0;
當前值比要比較值大輸出1;

2.3 基於比較器Comparator的比較

首先瞭解一下Comparator接口

public interface Comparator<T> {
    /**
     * Compares its two arguments for order.  Returns a negative integer,
     * zero, or a positive integer as the first argument is less than, equal
     * to, or greater than the second.<p>
     *
     * In the foregoing description, the notation
     * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
     * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
     * <tt>0</tt>, or <tt>1</tt> according to whether the value of
     * <i>expression</i> is negative, zero or positive.<p>
     *
     * The implementor must ensure that <tt>sgn(compare(x, y)) ==
     * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
     * implies that <tt>compare(x, y)</tt> must throw an exception if and only
     * if <tt>compare(y, x)</tt> throws an exception.)<p>
     *
     * The implementor must also ensure that the relation is transitive:
     * <tt>((compare(x, y)&gt;0) &amp;&amp; (compare(y, z)&gt;0))</tt> implies
     * <tt>compare(x, z)&gt;0</tt>.<p>
     *
     * Finally, the implementor must ensure that <tt>compare(x, y)==0</tt>
     * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
     * <tt>z</tt>.<p>
     *
     * It is generally the case, but <i>not</i> strictly required that
     * <tt>(compare(x, y)==0) == (x.equals(y))</tt>.  Generally speaking,
     * any comparator that violates this condition should clearly indicate
     * this fact.  The recommended language is "Note: this comparator
     * imposes orderings that are inconsistent with equals."
     *
     * @param o1 the first object to be compared.
     * @param o2 the second object to be compared.
     * @return a negative integer, zero, or a positive integer as the
     *         first argument is less than, equal to, or greater than the
     *         second.
     * @throws NullPointerException if an argument is null and this
     *         comparator does not permit null arguments
     * @throws ClassCastException if the arguments' types prevent them from
     *         being compared by this comparator.
     */
    int compare(T o1, T o2);

當然還有許多comparator實現的自定義比較方法,但這裡我隻貼出需要自己實現的方法compare;
接下來看看comparator的用法
當使用comparator時,如果要使用自定義的比較方式需要實現comparator接口,並且覆寫compare方法;因此需要自己構造一個比較器類實現comparator接口,然後利用我們自定義的比較器進行比較即可;
下面是一個應用實例

// write your code here
import java.util.*;
import java.lang.*;

public class Main{
    public static void main(String[] args){
        Card c1 = new Card(1,"♠");
        Card c2 = new Card(2,"♠");
        Card c3 = c1;
        CardComparator cardComparator = new CardComparator();
        System.out.println(cardComparator.compare(c1,c2));
        System.out.println(cardComparator.compare(c1,c3));
        System.out.println(cardComparator.compare(c2,c3));
    }
}
class  Card {
    public int rank;
    public String suit;
     public Card(int rank,String suit){
         this.rank = rank;
         this.suit = suit;
     }
}
class  CardComparator implements Comparator<Card>{

    @Override
    public int compare(Card o1, Card o2) {
        if (o1==o2){
            return 0;
        }
        if (o1==null)return -1;
        if (o2==null)return 1;
        return o1.rank-o2.rank;
    }
}

在這裡插入圖片描述

Comparator屬於java.util包中泛型接口類,使用時必須導入相關的包;
我們將Comparator中的compare方法重寫,就可以對需要進行對比的對象進行對比並返回結果。

2.4 幾種不同的compare對比

方法 說明
object.equals 直接覆寫即可,不過隻能比較相等與否
Comparable.compareTO 需要手動實現接口,當前類之後的所有對比方式都被定義,屬於內部順序
Comparator.compare 需要實現一個比較器對象,對待比較類的侵入性弱,但對代碼的侵入性強

到此這篇關於java對象對比之comparable和comparator的區別的文章就介紹到這瞭,更多相關comparable和comparator的區別內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: