1. 程式人生 > >Java 自定義比較器

Java 自定義比較器

Comparator 和 Comparable 相同點

  Comparator 和 Comparable都是java的介面,並且是用來對自定義的class比較大小的。

  對於自定義類如:public class Person{ String name; int age }.
  當我們有這麼一個personList,裡面包含了person1, person2, persion3…..,我們用Collections.sort( personList )是得不到預期的結果的。這時肯定有人要問,那為什麼可以排序一個字串list呢,如:
StringList{"hello1" , "hello3" , "hello2"} Collections.sort( stringList )


能夠得到正確的排序,那是因為String 這個物件已經幫我們實現了 Comparable介面 , 所以Person 如果想排序, 也要實現一個比較器。

Comparator 和 Comparable 的區別

Comparable

  對於Comparable介面來說,它往往是進行比較類需要實現的介面。
該比較器僅包含一個有compareTo()方法,只有一個引數,返回值為int。返回值大於0表示物件大於引數物件;小於0表示物件小於引數物件;等於0表示兩者相等。

  Comparable 實現在 Person類的內部:
public class Persion implements Comparable {..compareTo() 比較Person的大小..}


  因為已經實現了比較器,那麼Person現在是一個可以比較大小的物件了,它的比較功能和String完全一樣,可以隨時隨地的拿來比較大小,因為Person現在自身就是有大小之分的。Collections.sort(personList)可以得到正確的結果。

Comparator

  對於Comparator介面來說,它的實現者被稱為比較器,它包含一個compare()方法,有兩個引數,返回值與Comparable的compareTo()方法一樣,不同之處是Comparator介面一般不會被集合元素類所實現,而是單獨實現或者匿名內部類方式實現。

  Comparator 實現在Person的外部,此時Person類的結構不需要有任何變化,如:
public class Person{ String name; int age }


  然後另外定義一個比較器類
public PersonComparator implements Comparator {..compare() 比較Person的大小..}
  在PersonComparator裡面實現了怎麼比較兩個Person的大小。 所以,用這種方法,當要對一個 personList進行排序的時候,除了要傳遞personList過去,還需要把PersonComparator傳遞過去,因為怎麼比較Person的大小是在PersonComparator裡面實現的,如:Collections.sort( personList , new PersonComparator() )

示例如下:

import java.util.ArrayList;  
import java.util.Collections;  
import java.util.Comparator;  
import java.util.List;  

public class CompareTest {  
    public static void main(String[] args) {  
        List<Student> list = new ArrayList<Student>(10);  
        list.add(new Student(1,"jj"));  
        list.add(new Student(0,"ww"));  
        list.add(new Student(0,"kk"));  
        list.add(new Student(2,"ll"));  
        Collections.sort(list); // 內部比較器:要排序的類實現Comparable介面,可以對自身進行比較  
        System.out.println(list);  

        List<Teacher> t = new ArrayList<Teacher>(10);  
        t.add(new Teacher(1,12));  
        t.add(new Teacher(0,13));  
        t.add(new Teacher(0,14));  
        t.add(new Teacher(2,15));  
        Collections.sort(t,new StudentComparator()); //外部比較器:通過額外的類來實現Comparator介面  
        System.out.println(t);  
    }  
}  

Comparable

class Student implements Comparable {  
    int num;  
    String name;  

    public Student(int num, String name) {  
        this.num = num;  
        this.name = name;  
    }  

    @Override  
    public String toString() {  
        return "\r\tnum:"+num+" name:"+name+"\r";  
    }  

    public int compareTo(Object o) {  
        Student tmp = (Student) o;  
        int result = tmp.num > num ? 1 : (tmp.num==num ? 0 : -1);  
        if (result == 0) {  
            result = tmp.name.indexOf(0) > name.indexOf(0) ? 1 : -1;  
        }  
        return result;  
    }  
}  

Comparator

class Teacher{  
    int num;  
    double salary;  

    public Teacher(int num, double salary) {  
        this.num = num;  
        this.salary = salary;  
    }  

    @Override  
    public String toString() {  
        return "\r\tnum:"+num+" salary:"+salary+"\r";  
    }      
}  

class StudentComparator implements Comparator{  

    public int compare(Object o1, Object o2) {  
        Teacher t1 = (Teacher) o1;  
        Teacher t2 = (Teacher) o2;  
        int result = t1.num > t2.num ? 1 : (t1.num == t2.num ? 0 : -1);  
        return result = result == 0 ?(t1.salary<t2.salary ? 1 : -1) : result;  
    }   
}