Java中的comparable介面和Comparator介面的區別

來源:互聯網
上載者:User

標籤:rabl   null   ret   javadoc   compareto   怎麼辦   實現   node   pareto   

一.comparable和Comparator的區別

  1、Comparable和Comparator都是用來實現集合中元素的比較、排序的。

  2、Comparable是在類內部定義的方法實現的排序,位於java.lang下。

  3、Comparator是在類外部實現的排序,位於java.util下。

  4、實現Comparable介面需要覆蓋compareTo方法,實現Comparator介面需要覆蓋compare方法。

  5、Comparable介面將比較代碼嵌入需要進行比較的類的自身代碼中,而Comparator介面在一個獨立的類中實現比較。

二.深度認識

 首先Comparable這個介面是使用在你需要排序的元素類上的。

 而Comparator不是使用在你需要排序的元素類上的,它需要實現一個子類對象,將子類對象傳入可以排序的集合中(TreeSet)。

 當你想去使用comparable介面時必須去修改原始碼,因為他是建立在需要被排序的元素類上的。

那你要是想對String類進行你自己想要的排序怎麼辦呢?(舉例子,不要抬杠)

首先你不能去修改原始碼,第二String類是final類不能繼承。

那麼此時你就可以使用Comparator介面實現在類外的排序

三.執行個體

1.對元素類Node實現comparable介面

package com.cjm.lambda;import java.util.Set;import java.util.TreeSet;/** *  * @author 小明 * */public class SetSortlambda {    public static void main(String[] args) {        Set<Node> set = new TreeSet<>();        set.add(new Node("zs",10));        set.add(new Node("zs1",110));        set.add(new Node("zs1",10));        set.add(new Node("zszx",100));        set.add(new Node("zzxs",10));        System.out.println(set);    }}/* * 元素本生實現comparator介面 */class Node implements Comparable<Node> {    int num;    String str;    public Node(String str,int num) {        this.str=str;        this.num = num;    }    @Override    public int compareTo(Node o) {        if (this.str == null) {            return -1;        } else {            if (o.str == null) {                return 1;            } else {                if (this.str.length() < o.str.length()) {                    return -1;                } else {                    if (this.str.length() > o.str.length()) {                        return 1;                    } else {                        if (this.num < o.num) {                            return -1;                        } else {                            if (this.num > o.num) {                                return 1;                            } else {                                return 0;                            }                        }                    }                }            }        }    }    /* (non-Javadoc)     * @see java.lang.Object#toString()     */    @Override    public String toString() {        return  str+num;    }}

 

2.使用Comparator介面,實現一個Comparator介面的對象(採用內部類的方式)

package com.cjm.lambda;import java.util.Comparator;import java.util.Set;import java.util.TreeSet;/** * Compararator介面實現排序 *  * @author 小明 * */public class Text {    public static void main(String[] args) {        Set<Node> set = new TreeSet<>(new Comparator<Node>() {            @Override            public int compare(Node node1, Node node2) {                if (node1.str.length() < node2.str.length()) {                    return 1;                } else {                    if (node1.str.length() > node2.str.length()) {                        return -1;                    } else {                        if (node1.num < node2.num) {                            return 1;                        } else {                            if (node1.num > node2.num) {                                return -1;                            } else {                                return 0;                            }                        }                    }                }            }        });        set.add(new Node("zs", 10));        set.add(new Node("zs1", 110));        set.add(new Node("zs1", 10));        set.add(new Node("zszx", 100));        set.add(new Node("zzxs", 10));        System.out.println(set);    }}

 

Java中的comparable介面和Comparator介面的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.