比较器Comparator 和 Comparable的简单区别

来源:互联网 发布:逻辑回归算法概念 编辑:程序博客网 时间:2024/06/15 08:41

  例如现在有一个自定义的类,

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. class Score{  
  2.   public int score;  
  3.   public int time;  
  4.     
  5.     public int getScore() {  
  6.     return score;  
  7. }  
  8.   
  9. public void setScore(int score) {  
  10.     this.score = score;  
  11. }  
  12.   
  13. public int getTime() {  
  14.     return time;  
  15. }  
  16.   
  17. public void setTime(int time) {  
  18.     this.time = time;  
  19. }  
  20.   
  21.     public Score(int score, int time) {  
  22.         super();  
  23.         this.score = score;  
  24.         this.time = time;  
  25.     }  
  26.       
  27.       
  28. }  
如果对一个对象数组进行排序

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Score[] score=new Score[n];  
  2. Arrays.sort(score);  
这样是没法实现的,而对基本的数据类型可以这样排序是因为其都实现了Comparable<T>接口

例如

 

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public final class Integer extends Number implements <strong>Comparable<Integer></strong> {  
  2.   
  3.   
  4. public final class String  
  5.     implements java.io.Serializable, <strong>Comparable<String></strong>, CharSequence {  
所以这里的Score对象必须先实现自己的比较器才能用上述类似的方式进行排序


2,比较

    (1).Comparator 和 Comparable都是Java中的内部比较器接口,都是用来实现对一个自定义的类进行排序

    (2). 不同的是实现Comparable接口是定义在类的内部,比较代码需要嵌入类的内部结构中

          Comparator 实现在类的外部,单独实现第一个比较器,不需要对原来的类进行结构上的变化,属于无侵入式的。

  具体到上面的例子

     Comparable<T>内部侵入式实现比较器

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. class Score implements Comparable<Score>{  
  2.   public int score;  
  3.   public int time;  
  4.     
  5.     public int getScore() {  
  6.     return score;  
  7. }  
  8.   
  9. public void setScore(int score) {  
  10.     this.score = score;  
  11. }  
  12.   
  13. public int getTime() {  
  14.     return time;  
  15. }  
  16.   
  17. public void setTime(int time) {  
  18.     this.time = time;  
  19. }  
  20.   
  21.     @Override  
  22.     public int compareTo(Score o) {  
  23.         if(this.time>o.time) return 1;  
  24.         else if(this.time==o.time) return 0;  
  25.         else return -1;  
  26.     }  
  27.   
  28.     public Score(int score, int time) {  
  29.         super();  
  30.         this.score = score;  
  31.         this.time = time;  
  32.     }  
  33. }   
然后在主类中直接比较 Arrays.sort(score);


  Comparator <T>无侵入式实现比较器,只需要单独写一个比较器实现类ScoreComparator,或者写成匿名类形式

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.    /*
  2.      单独写成一个类
  3. */
  4. class ScoreComparator implements Comparator<Score>{  
  5.   
  6.     @Override  
  7.     public int compare(Score o1, Score o2) {  
  8.         if(o1.time>o2.time) return 1;  
  9.         else if(o1.time==o2.time) return 0;  
  10.         else return -1;  
  11.     }     
  12. }  

然后在主类中带入比较器类Arrays.sort(score, new ScoreComparator());

也可以写成匿名类:

Arrays.sort(score, new Comparator<TestComparator>() {@Overridepublic int compare(TestComparator o1, TestComparator o2) {  //按逆序排序if (o1.time < o2.time)    return 1;else if (o1.time > o2.time)return -1;else {return 0;}}});


0 0
原创粉丝点击