天天看點

7 list排序

Collections對List集合中的資料進行排序

有時候需要對集合中的元素按照一定的規則進行排序,這就需要用到

Java中提供的對集合進行操作的工具類Collections,其中的sort方法

先看一個簡單的例子:

[java]  view plain  copy

  1. public static void main(String[] args) {  
  2.     List<Integer> nums = new ArrayList<Integer>();  
  3.         nums.add(3);  
  4.         nums.add(5);  
  5.         nums.add(1);  
  6.         nums.add(0);  
  7.         System.out.println(nums);  
  8.         Collections.sort(nums);  
  9. }  

輸出結果:

[3, 5, 1, 0]

[0, 1, 3, 5]

  1. package core.java.collection.collections;  
  2. public class User implements Comparable<User>{  
  3.     private int score;  
  4.     private int age;  
  5.     public User(int score, int age){  
  6.         super();  
  7.         this.score = score;  
  8.         this.age = age;  
  9.     }  
  10.     public int getScore() {  
  11.         return score;  
  12.     public void setScore(int score) {  
  13.     public int getAge() {  
  14.         return age;  
  15.     public void setAge(int age) {  
  16.     @Override  
  17.     public int compareTo(User o) {  
  18.         int i = this.getAge() - o.getAge();//先按照年齡排序  
  19.         if(i == 0){  
  20.             return this.score - o.getScore();//如果年齡相等了再用分數進行排序  
  21.         }  
  22.         return i;  
  23.         List<User> users = new ArrayList<User>();  
  24.         users.add(new User(78, 26));  
  25.         users.add(new User(67, 23));  
  26.         users.add(new User(34, 56));  
  27.         users.add(new User(55, 23));  
  28.         Collections.sort(users);  
  29.         for(User user : users){  
  30.             System.out.println(user.getScore() + "," + user.getAge());  

55,23

67,23

78,26

34,56

我們會發現sort(List<T>)方法中List中的T必須實作Comparable<T>接口,然後實作

compareTo()方法,該方法的傳回值0代表相等,1表示大于,-1表示小于;為什麼

在簡單例子中沒有看到實作Comparable接口呢?是因為Integer類其實自己已經實作

了Comparable接口,Java已經給我們做好了。

Collections提供的第二種排序方法sort(List<T> list, Comparator<? super T> c)

先看例子:

  1. public class Students {  
  2.     public Students(int age, int score){  
  3.         List<Students> students = new ArrayList<Students>();  
  4.         students.add(new Students(23, 100));  
  5.         students.add(new Students(27, 98));  
  6.         students.add(new Students(29, 99));  
  7.         students.add(new Students(29, 98));  
  8.         students.add(new Students(22, 89));  
  9.         Collections.sort(students, new Comparator<Students>() {  
  10.             @Override  
  11.             public int compare(Students o1, Students o2) {  
  12.                 int i = o1.getScore() - o2.getScore();  
  13.                 if(i == 0){  
  14.                     return o1.getAge() - o2.getAge();  
  15.                 }  
  16.                 return i;  
  17.             }  
  18.         });  
  19.         for(Students stu : students){  
  20.             System.out.println("score:" + stu.getScore() + ":age" + stu.getAge());  

score:89:age22

score:98:age27

score:98:age29

score:99:age29

score:100:age23

從上面的例子我們可以看出Students類沒有實作Comparable<T>接口,隻是在sort()方法

中多傳入一個參數,隻不過該參數是一個接口我們需要實作其compare方法。

以上就是是Java中Colelctions工具類為我們提供的兩種集合排序方法。

上一篇: list的排序
下一篇: Java List 排序