Comparator和Comparable比較
阿新 • • 發佈:2019-02-03
當需要排序的集合或陣列不是單純的數字型時,通常可以使用Comparator或Comparable,以簡單的方式實現物件排序或自定義排序。
3*/4int compare(Object o1, Object o2);
2import java.util.Comparator;
3
4publicclass SampleComparator implements Comparator {
5
6public int compare(Object o1, Object o2) {
7return toInt(o1) - toInt(o2);
8 }9
10privateint toInt(Object o) {
11 String str = (String) o;
12 str = str.replaceAll("一", "1");
13 str = str.replaceAll("二", "2");
14 str = str.replaceAll("三", "3");
15return Integer.parseInt(str);
16 }17
18/** *//**19 * 測試方法
20*/21publicstaticvoid main(String[] args) {
22 String[] array =new String[] { "一二", "三", "二" };
23 Arrays.sort(array, new SampleComparator());
24for (int i =0; i < array.length; i++) {
25 System.out.println(array[i]);
26 }27 }28
29}
1/** *//**2 * @return 該物件小於、等於或大於指定物件o,分別返回負整數、零或正整數。
3*/4int compareTo(Object o);
2
3private String id;
4privateint age;
5
6public User(String id, int age) {
7this.id = id;
8this.age = age;
9 }10
11publicint getAge() {
12return age;
13 }14
15publicvoid setAge(int age) {
16this.age = age;
17 }18
19public String getId() {
20return id;
21 }22
23publicvoid setId(String id) {
24this.id = id;
25 }26
27}
2
3publicclass User implements Comparable {
4
5private String id;
6privateint age;
7
8public User(String id, int age) {
9this.id = id;
10this.age = age;
11 }12
13publicint getAge() {
14return age;
15 }16
17publicvoid setAge(int age) {
18this.age = age;
19 }20
21public String getId() {
22return id;
23 }24
25publicvoid setId(String id) {
26this.id = id;
27 }28
29publicint compareTo(Object o) {
30returnthis.age - ((User) o).getAge();
31 }32
33/** *//**34 * 測試方法
35*/36publicstaticvoid main(String[] args) {
37 User[] users =new User[] { new User("a", 30), new User("b", 20) };
38 Arrays.sort(users);
39for (int i =0; i < users.length; i++) {
40 User user = users[i];
41 System.out.println(user.getId() +""+ user.getAge());
42 }43 }44
45}
2import java.util.Comparator;
3
4publicclass UserComparator implements Comparator {
5
6publicint compare(Object o1, Object o2) {
7return ((User) o1).getAge() - ((User) o2).getAge();
8 }9
10/** *//**11 * 測試方法
12*/13publicstaticvoid main(String[] args) {
14 User[] users =new User[] { new User("a", 30), new User("b", 20) };
15 Arrays.sort(users, new UserComparator());
16for (int i =0; i < users.length; i++) {
17 User user = users[i];
18 System.out.println(user.getId() +""+ user.getAge());
19 }20 }21
22}
一、Comparator
強行對某個物件collection進行整體排序的比較函式,可以將Comparator傳遞給Collections.sort或Arrays.sort。
介面方法:
1/** *//**2 * @return o1小於、等於或大於o2,分別返回負整數、零或正整數。3*/4int compare(Object o1, Object o2);
案例:
1import java.util.Arrays;2import java.util.Comparator;
3
4publicclass SampleComparator implements Comparator {
5
6public
7return toInt(o1) - toInt(o2);
8 }9
10privateint toInt(Object o) {
11 String str = (String) o;
12 str = str.replaceAll("一", "1");
13 str = str.replaceAll("二", "2");
14 str = str.replaceAll("三", "3");
15return Integer.parseInt(str);
16 }17
18/** *//**19 * 測試方法
20*/21publicstaticvoid
22 String[] array =new String[] { "一二", "三", "二" };
23 Arrays.sort(array, new SampleComparator());
24for (int i =0; i < array.length; i++) {
25 System.out.println(array[i]);
26 }27 }28
29}
二、Comparable
強行對實現它的每個類的物件進行整體排序,實現此介面的物件列表(和陣列)可以通過Collections.sort或Arrays.sort進行自動排序。
介面方法:
3*/4int compareTo(Object o);
假設物件User,需要按年齡排序:
1publicclass User {2
3private String id;
4privateint age;
5
6public User(String id, int age) {
7this.id = id;
8this.age = age;
9 }10
11publicint getAge() {
12return age;
13 }14
15publicvoid setAge(int age) {
16this.age = age;
17 }18
19public String getId() {
20return id;
21 }22
23publicvoid setId(String id) {
24this.id = id;
25 }26
27}
改造後的物件:
2
3publicclass User implements Comparable {
4
5private String id;
6privateint age;
7
8public User(String id, int age) {
9this.id = id;
10this.age = age;
11 }12
13publicint getAge() {
14return age;
15 }16
17publicvoid setAge(int age) {
18this.age = age;
19 }20
21public String getId() {
22return id;
23 }24
25publicvoid setId(String id) {
26this.id = id;
27 }28
29publicint compareTo(Object o) {
30returnthis.age - ((User) o).getAge();
31 }32
33/** *//**34 * 測試方法
35*/36publicstaticvoid main(String[] args) {
37 User[] users =new User[] { new User("a", 30), new User("b", 20) };
38 Arrays.sort(users);
39for (int i =0; i < users.length; i++) {
40 User user = users[i];
41 System.out.println(user.getId() +""+ user.getAge());
42 }43 }44
45}
三、Comparator和Comparable的區別
先看一下使用Comparator對User集合實現排序的方式:
1import java.util.Arrays;2import java.util.Comparator;
3
4publicclass UserComparator implements Comparator {
5
6publicint compare(Object o1, Object o2) {
7return ((User) o1).getAge() - ((User) o2).getAge();
8 }9
10/** *//**11 * 測試方法
12*/13publicstaticvoid main(String[] args) {
14 User[] users =new User[] { new User("a", 30), new User("b", 20) };
15 Arrays.sort(users, new UserComparator());
16for (int i =0; i < users.length; i++) {
17 User user = users[i];
18 System.out.println(user.getId() +""+ user.getAge());
19 }20 }21
22}
一個類實現了Camparable介面則表明這個類的物件之間是可以相互比較的,這個類物件組成的集合就可以直接使用sort方法排序。
Comparator可以看成一種演算法的實現,將演算法和資料分離,Comparator也可以在下面兩種環境下使用:
1、類的設計師沒有考慮到比較問題而沒有實現Comparable,可以通過Comparator來實現排序而不必改變物件本身
2、可以使用多種排序標準,比如升序、降序等