1. 程式人生 > 實用技巧 >Comparable和Comparator介面的使用

Comparable和Comparator介面的使用

一、Comparable介面,需要某一個類實現該介面,介面中複寫comparaTo(<T> o)方法,自己寫比較規則,示例程式碼如下

Person類,實現了Comparable介面,複寫了comparaTo方法

 1 package cn.gao.day1019;
 2 
 3 public class Person implements Comparable<Person> {
 4     private String name;
 5     private int age;
 6     private double weight;
 7     private
double height; 8 9 public Person() { 10 } 11 12 public Person(String name, int age, double weight, double height) { 13 this.name = name; 14 this.age = age; 15 this.weight = weight; 16 this.height = height; 17 } 18 19 public String getName() {
20 return name; 21 } 22 23 public void setName(String name) { 24 this.name = name; 25 } 26 27 public int getAge() { 28 return age; 29 } 30 31 public void setAge(int age) { 32 this.age = age; 33 } 34 35 public double getWeight() { 36 return
weight; 37 } 38 39 public void setWeight(double weight) { 40 this.weight = weight; 41 } 42 43 public double getHeight() { 44 return height; 45 } 46 47 public void setHeight(double height) { 48 this.height = height; 49 } 50 51 @Override 52 public String toString() { 53 return "Person{" + 54 "name='" + name + '\'' + 55 ", age=" + age + 56 ", weight=" + weight + 57 ", height=" + height + 58 '}'; 59 } 60 61 @Override 62 public int compareTo(Person o) { 63 if(this.age<o.age){ 64 return -1; 65 } 66 else if (this.age>o.age){ 67 return 1; 68 } 69 return 0; 70 } 71 }

在測試類中,如果要排序該型別的陣列,可以直接呼叫Arrays.sort()方法,即可實現按自己寫的規則排序。

 1 package cn.gao.day1019;
 2 
 3 import java.util.Arrays;
 4 
 5 public class PersonDemo1 {
 6     public static void main(String[] args) {
 7         Person[] per = {new Person("zhangsan",20,75.5,165.5),
 8                 new Person("lisi",25,88.5,193.4),
 9                 new Person("wangwu",18,60.5,175.7),
10                 new Person("zhaoliu",23,72.4,181.2),
11                 new Person("chenqi",16,53.5,162.5)};
12         Arrays.sort(per);
13         for (Person person : per) {
14             System.out.println(person);
15         }
16     }
17 }

列印結果如下:可以看到按照comparaTo()中的櫃子,按照年齡的升序進行了排列。

Person{name='chenqi', age=16, weight=53.5, height=162.5}

Person{name='wangwu', age=18, weight=60.5, height=175.7}

Person{name='zhangsan', age=20, weight=75.5, height=165.5}

Person{name='zhaoliu', age=23, weight=72.4, height=181.2}

Person{name='lisi', age=25, weight=88.5, height=193.4}

二、Comparator需要根據不同屬性的比較,分別寫不同的實現類。

Person類不變,還是上面的原樣,按Age比較的類如下

 1 package cn.gao.day1019;
 2 
 3 import java.util.Comparator;
 4 
 5 /**
 6  * 實現按age排序的類
 7  */
 8 public class PersonSortByAge implements Comparator<Person> {
 9     @Override
10     public int compare(Person o1, Person o2) {
11         if (o1.getAge() < o2.getAge()) {
12             return -1;
13         } else if (o1.getAge() > o2.getAge()) {
14             return 1;
15         }
16         return 0;
17     }
18 }

按Height比較的類如下

 1 package cn.gao.day1019;
 2 
 3 import java.util.Comparator;
 4 
 5 /**
 6  * 實現按height比較的類
 7  */
 8 public class PersonSortByHeight implements Comparator<Person> {
 9     @Override
10     public int compare(Person o1, Person o2) {
11         if (o1.getHeight() < o2.getHeight()) {
12             return -1;
13         } else if (o1.getHeight() > o2.getHeight()) {
14             return 1;
15         }
16         return 0;
17     }
18 }

按Weight比較的類如下

 1 package cn.gao.day1019;
 2 
 3 import java.util.Comparator;
 4 
 5 /**
 6  * 實現按weight比較的類
 7  */
 8 public class PersonSortByWeight implements Comparator<Person> {
 9     @Override
10     public int compare(Person o1, Person o2) {
11         if (o1.getWeight() < o2.getWeight()) {
12             return -1;
13         } else if (o1.getWeight() > o2.getWeight()) {
14             return 1;
15         }
16         return 0;
17     }
18 }

測試類如下

 1 package cn.gao.day1019;
 2 
 3 import java.util.Arrays;
 4 import java.util.Comparator;
 5 
 6 public class PersonDemo2 {
 7     public static void main(String[] args) {
 8         Person[] per = {new Person("zhangsan", 20, 75.5, 165.5),
 9                 new Person("lisi", 25, 88.5, 193.4),
10                 new Person("wangwu", 18, 60.5, 175.7),
11                 new Person("zhaoliu", 23, 72.4, 181.2),
12                 new Person("chenqi", 16, 53.5, 162.5)};
13         System.out.println("按age排序");
14         Arrays.sort(per, new PersonSortByAge());
15         for (Person person : per) {
16             System.out.println(person);
17         }
18         System.out.println("按height排序");
19         Arrays.sort(per, new PersonSortByHeight());
20         for (Person person : per) {
21             System.out.println(person);
22         }
23         System.out.println("按weight排序");
24         Arrays.sort(per, new PersonSortByWeight());
25         for (Person person : per) {
26             System.out.println(person);
27         }
28     }
29 }

執行結果如下:

按age排序
Person{name='chenqi', age=16, weight=53.5, height=162.5}
Person{name='wangwu', age=18, weight=60.5, height=175.7}
Person{name='zhangsan', age=20, weight=75.5, height=165.5}
Person{name='zhaoliu', age=23, weight=72.4, height=181.2}
Person{name='lisi', age=25, weight=88.5, height=193.4}
按height排序
Person{name='chenqi', age=16, weight=53.5, height=162.5}
Person{name='zhangsan', age=20, weight=75.5, height=165.5}
Person{name='wangwu', age=18, weight=60.5, height=175.7}
Person{name='zhaoliu', age=23, weight=72.4, height=181.2}
Person{name='lisi', age=25, weight=88.5, height=193.4}
按weight排序
Person{name='chenqi', age=16, weight=53.5, height=162.5}
Person{name='wangwu', age=18, weight=60.5, height=175.7}
Person{name='zhaoliu', age=23, weight=72.4, height=181.2}
Person{name='zhangsan', age=20, weight=75.5, height=165.5}
Person{name='lisi', age=25, weight=88.5, height=193.4}

可以看出,結果已經分別按age、height、weight進行了排序

總結:Comparable介面,實現該介面後,需要實現介面中的comparaTo方法,自身實現了排序,就可以和基本資料型別陣列一樣,使用Arrays.sort()方法進行排序了,傳遞引數為要排序的陣列物件。只能定義一種比較規則,如果比較規則發生改變,那麼就需要改動comparaTo()方法內的內容。

Comparator介面,該介面是單獨的一個類去實現,即提前準備好不同引數的比較方法,有幾套方法,就提前寫接個比較的類,也可以後期擴充套件比較的類。排序時,也呼叫Arrays.sort(),但是傳遞的引數除了陣列物件外,還要傳遞一個比較器物件。

Comparable介面較為單一,如果比較的引數固定,不存在多種情況,那麼採用這種方法即可,只需要類裡面實現comparaTo()方法,呼叫時直接傳遞待排序陣列即可。

Comparable介面是預定義幾種排序的類,同時後期可以根據業務擴充套件更多的排序類,擴充套件性強,呼叫時除了待排序的方法,還要傳遞排序類物件。