比较器Comparator 和 Comparable的简单区别

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

[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>() {

			@Override
			public int compare(TestComparator o1, TestComparator o2) {  //按逆序排序

				if (o1.time < o2.time)    
					return 1;
				else if (o1.time > o2.time)
					return -1;
				else {
					return 0;
				}
			}

		});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值