Java中Comparable与Comparator的区别

1、Comparable

如果想让一个类的实例之间可以相互比较的,那么这个类就必须要实现Comparable接口,并且覆盖compareTo()方法

 1 public class Main1{
 2     public static void main(String[] args){
 3         HDTV tv1 = new HDTV(32,"Changhong");
 4         HDTV tv2 = new HDTV(35,"Skyworth");
 5         System.out.println(tv1.compareTo(tv2));
 6     }
 7 }
 8 
 9 class HDTV implements Comparable<HDTV>{
10     private int size;
11     private String brand;
12     
13     public HDTV(int size,String brand){
14         this.size = size;
15         this.brand = brand;
16     }
17     
18     public int getSize(){
19         return this.size;
20     }
21     
22     public void setSize(int size){
23         this.size = size;
24     }
25     
26     public String getBrand(){
27         return this.brand;
28     }
29     
30     public void setBrand(String brand){
31         this.brand = brand;
32     }
33     
34     @Override
35     public int compareTo(HDTV tv){
36         if(this.getSize()>tv.getSize())
37             return 1;
38         else if(this.getSize()<tv.getSize())
39             return -1;
40         else
41             return 0;
42     }
43     
44     
45 }

2、Comparator

在某些情况下,你可能不想让这个类具有比较性,在这样的案例中,你可能需要比较类中的某些字段;例如,2个人之间可以比较他们的身高和年龄;

这样的情况下,你就需要用到Comparator接口,并且实现compare()方法;使用Comparator的另外一个共同特性就是排序,Collections和Arrays工具类提供的接受比较器参数的排序方法

 1 import java.util.*;
 2 public class Main2{
 3     public static void main(String[] args){
 4         HPTV tv1 = new HPTV(36,"SamSung");
 5         HPTV tv2 = new HPTV(21,"Sony");
 6         HPTV tv3 = new HPTV(28,"Dell");
 7         
 8         ArrayList<HPTV> list = new ArrayList<HPTV>();
 9         list.add(tv1);
10         list.add(tv2);
11         list.add(tv3);
12         
13         Collections.sort(list, new BrandComparator());
14         
15         for(HPTV tv: list)
16             System.out.println(tv.getSize()+"--"+tv.getBrand());
17     }
18     
19 }
20 class HPTV{
21     private int size;
22     private String brand;
23     
24     public HPTV(int size,String brand){
25         this.size = size;
26         this.brand = brand;
27     }
28     
29     public int getSize(){
30         return this.size;
31     }
32     
33     public void setSize(int size){
34         this.size = size;
35     }
36     
37     public String getBrand(){
38         return this.brand;
39     }
40     
41     public void setBrand(String brand){
42         this.brand = brand;
43     }
44     
45 }
46 
47 class SizeComparator implements Comparator<HPTV>{
48     @Override
49     public int compare(HPTV tv1,HPTV tv2){
50         int tv1Size = tv1.getSize();
51         int tv2Size = tv2.getSize();
52         if(tv1Size>tv2Size)
53             return 1;
54         else if(tv1Size<tv2Size)
55             return  -1;
56         else 
57             return 0;
58     }
59 }
60 class BrandComparator implements Comparator<HPTV>{
61     @Override
62     public int compare(HPTV tv1, HPTV tv2){
63         String nm1 = tv1.getBrand();
64         String nm2 = tv2.getBrand();
65         return nm1.compareTo(nm2);
66     }
67 }

有时,我们经常用到 Collections.reverseOrder()方法,这个方法对list进行反转

3、使用的时候怎么选择呢?

Comparable:如果一个类实现了Comaprable接口,那么这个类的实例之间可以互相比较;

Comparator:继承 Comparator 接口有两种情况

  1)作为参数传递给排序方法:Collections.sort() or Arrays.sort()精确的控制排序规则

  2)精确的排序控制一些固定的数据结构,TreeSet和TreeMap

案例-TreeSet-Comparator

 1 class Dog {
 2     int size;
 3  
 4     Dog(int s) {
 5         size = s;
 6     }
 7 }
 8  
 9 class SizeComparator implements Comparator<Dog> {
10     @Override
11     public int compare(Dog d1, Dog d2) {
12         return d1.size - d2.size;
13     }
14 }
15  
16 public class ImpComparable {
17     public static void main(String[] args) {
18         TreeSet<Dog> d = new TreeSet<Dog>(new SizeComparator()); // pass comparator
19         d.add(new Dog(1));
20         d.add(new Dog(2));
21         d.add(new Dog(1));
22     }
23 }

案例-TreeSet-Comparable

 1 class Dog implements Comparable<Dog>{
 2     int size;
 3  
 4     Dog(int s) {
 5         size = s;
 6     }
 7  
 8     @Override
 9     public int compareTo(Dog o) {
10         return o.size - this.size;
11     }
12 }
13  
14 public class ImpComparable {
15     public static void main(String[] args) {
16         TreeSet<Dog> d = new TreeSet<Dog>();
17         d.add(new Dog(1));
18         d.add(new Dog(2));
19         d.add(new Dog(1));
20     }
21 }
原文地址:https://www.cnblogs.com/lianliang/p/5388023.html