1. 程式人生 > >Java中的comparable接口和Comparator接口的區別

Java中的comparable接口和Comparator接口的區別

java tor 舉例 compare 不能 style void doc r+

一.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接口的區別