Java容器排序的方法

来源:互联网 发布:网络杀机 百度云 编辑:程序博客网 时间:2024/06/07 05:58

转自:http://blog.csdn.net/shirenfeigui/article/details/39051741


首先说一下排序的返回值的含义。对于参与比较的两个Object,o1和o2,如果函数的返回值为正值,把o1排在o2后面;返回值为负值,把o1排在o2前面。如果返回值是0,按照容器之前的顺序排列。在compareTo中,this相当于o1,传入的Object相当于o2

第一种方法:对于要排序的类实现Comparable接口

  1. package sort;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.Iterator;  
  6. import java.util.List;  
  7. //采用实现Comparable接口的方法实现排序  
  8. class S1 implements Comparable{  
  9.     int x;  
  10.     int y;  
  11.     S1(int x, int y){  
  12.         this.x = x;  
  13.         this.y = y;  
  14.     }  
  15.     //实现排序方法。先比较x,如果相同比较y  
  16.     @Override  
  17.     public int compareTo(Object o) {  
  18.         S1 obj = (S1) o;  
  19.         if(x != obj.x)  
  20.         {  
  21.             return x - obj.x;  
  22.         }  
  23.         return y - obj.y;  
  24.     }  
  25.     //重写toStirng方法,改变println时的显示效果  
  26.     public String toString(){  
  27.         return "("+x+", "+y+")";  
  28.     }  
  29. }  
  30.   
  31. public class Sort1 {  
  32.     public static void main(String[] args) {  
  33.         List<S1> s1Set = new ArrayList<S1>();  
  34.         S1 s1 = new S1(3,5);  
  35.         S1 s2 = new S1(2,5);  
  36.         S1 s3 = new S1(2,2);  
  37.         s1Set.add(s1);  
  38.         s1Set.add(s2);  
  39.         s1Set.add(s3);  
  40.         //对容器进行排序的函数  
  41.         Collections.sort(s1Set);  
  42.         Iterator it = s1Set.iterator();  
  43.         while(it.hasNext())  
  44.         {  
  45.             System.out.println(it.next());  
  46.         }  
  47.     }  
  48. }

第二种方法:覆盖Comparator中的compare方法。

  1. package sort;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.Comparator;  
  6. import java.util.Iterator;  
  7. import java.util.List;  
  8.   
  9. class S2{  
  10.     int x;  
  11.     int y;  
  12.     S2(int x, int y){  
  13.         this.x = x;  
  14.         this.y = y;  
  15.     }  
  16.     //重写toStirng方法,改变println时的显示效果  
  17.     public String toString(){  
  18.         return "("+x+", "+y+")";  
  19.     }  
  20. }  
  21.   
  22. public class Sort2 {  
  23.     public static void main(String[] args) {  
  24.         List<S2> s2Set = new ArrayList<S2>();  
  25.         S2 s1 = new S2(3,5);  
  26.         S2 s2 = new S2(4,5);  
  27.         S2 s3 = new S2(4,2);  
  28.         s2Set.add(s1);  
  29.         s2Set.add(s2);  
  30.         s2Set.add(s3);  
  31.         //对容器进行排序的函数  
  32.         Collections.sort(s2Set,c);  
  33.         Iterator it = s2Set.iterator();  
  34.         while(it.hasNext())  
  35.         {  
  36.             System.out.println(it.next());  
  37.         }     
  38.     }  
  39.     static Comparator<S2> c = new Comparator(){  
  40.         public int compare(Object a0, Object a1) {  
  41.             S2 s1 = (S2) a0;  
  42.             S2 s2 = (S2) a1;  
  43.             if(s1.x != s2.x)  
  44.             {  
  45.                 return s1.x - s2.x;  
  46.             }  
  47.             else  
  48.             {  
  49.                 return s1.y - s2.y;  
  50.             }  
  51.         }  
  52.     };  
  53.       
  54. }

原创粉丝点击