黑马程序员_java--comparable和Comparator 的区别

来源:互联网 发布:php csrf 编辑:程序博客网 时间:2024/05/22 17:49
------- android培训、java培训、期待与您交流! ----------

Comparable & Comparator 都是用来实现集合中的排序的,只是 Comparable 是在集合内部定义的方法实现的排序,Comparator 是在集合外部实现的排序,所以,如想实现排序,就需要在集合外定义 Comparator 接口的方法或在集合内实现 Comparable 接口的方法。当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comparator或Comparable,以简单的方式实现对象排序或自定义排序。
一、Comparator

强行对某个对象collection进行整体排序的比较函数,可以将Comparator传递给Collections.sort或Arrays.sort。

接口方法:


Java代码  收藏代码
  1. /** 
  2.  * @return o1小于、等于或大于o2,分别返回负整数、零或正整数。 
  3.  */  
  4. int compare(Object o1, Object o2);  



案例:

  1. import java.util.Arrays;  
  2. import java.util.Comparator;  
  3.   
  4. public class SampleComparator implements Comparator {  
  5.   
  6.   public int compare(Object o1, Object o2) {  
  7.     return toInt(o1) - toInt(o2);  
  8.   }  
  9.   
  10.   private int toInt(Object o) {  
  11.     String str = (String) o;  
  12.     str = str.replaceAll("一""1");  
  13.     str = str.replaceAll("二""2");  
  14.     str = str.replaceAll("三""3");  
  15.     //   
  16.     return Integer.parseInt(str);  
  17.   }  
  18.   
  19.   /** 
  20.    * 测试方法 
  21.    */  
  22.   public static void main(String[] args) {  
  23.     String[] array = new String[] { "一二""三""二" };  
  24.     Arrays.sort(array, new SampleComparator());  
  25.     for (int i = 0; i < array.length; i++) {  
  26.       System.out.println(array[i]);  
  27.     }  
  28.   }  
  29.   
  30. }  


二、Comparable

强行对实现它的每个类的对象进行整体排序,实现此接口的对象列表(和数组)可以通过Collections.sort或Arrays.sort进行自动排序。

接口方法:


  1. /** 
  2.  * @return 该对象小于、等于或大于指定对象o,分别返回负整数、零或正整数。  
  3.  */  
  4. int compareTo(Object o);  


假设对象User,需要按年龄排序:

  1. public class User {  
  2.   
  3.   private String id;  
  4.   private int age;  
  5.   
  6.   public User(String id, int age) {  
  7.     this.id = id;  
  8.     this.age = age;  
  9.   }  
  10.   
  11.   public int getAge() {  
  12.     return age;  
  13.   }  
  14.   
  15.   public void setAge(int age) {  
  16.     this.age = age;  
  17.   }  
  18.   
  19.   public String getId() {  
  20.     return id;  
  21.   }  
  22.   
  23.   public void setId(String id) {  
  24.     this.id = id;  
  25.   }  
  26.   
  27. }  


改造后的对象:

  1. import java.util.Arrays;  
  2.   
  3. public class User implements Comparable {  
  4.   
  5.   private String id;  
  6.   private int age;  
  7.   
  8.   public User(String id, int age) {  
  9.     this.id = id;  
  10.     this.age = age;  
  11.   }  
  12.   
  13.   public int getAge() {  
  14.     return age;  
  15.   }  
  16.   
  17.   public void setAge(int age) {  
  18.     this.age = age;  
  19.   }  
  20.   
  21.   public String getId() {  
  22.     return id;  
  23.   }  
  24.   
  25.   public void setId(String id) {  
  26.     this.id = id;  
  27.   }  
  28.   
  29.   public int compareTo(Object o) {  
  30.     return this.age - ((User) o).getAge();  
  31.   }  
  32.   
  33.   /** 
  34.    * 测试方法 
  35.    */  
  36.   public static void main(String[] args) {  
  37.     User[] users = new User[] { new User("a"30), new User("b"20) };  
  38.     Arrays.sort(users);  
  39.     for (int i = 0; i < users.length; i++) {  
  40.       User user = users[i];  
  41.       System.out.println(user.getId() + " " + user.getAge());  
  42.     }  
  43.   }  
  44.   
  45. }  



 

------- android培训、java培训、期待与您交流! ----------