Collections对List集合中的数据进行排序

有时候需要对集合中的元素按照一定的规则进行排序,这就需要用到

Java中提供的对集合进行操作的工具类Collections,其中的sort方法


先看一个简单的例子:

[java] view plain copy print ?
  1. public static void main(String[] args) {  
  2.     List nums = new ArrayList();  
  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.         System.out.println(nums);  
  10. }  
public static void main(String[] args) {    List nums = new ArrayList();        nums.add(3);        nums.add(5);        nums.add(1);        nums.add(0);        System.out.println(nums);        Collections.sort(nums);        System.out.println(nums);}
输出结果:
[3, 5, 1, 0]
[0, 1, 3, 5]

稍微复杂的List里面放一个复杂的对象

[java] view plain copy print ?
  1. package core.java.collection.collections;  
  2.   
  3. public class User implements Comparable{  
  4.       
  5.     private int score;  
  6.       
  7.     private int age;  
  8.       
  9.     public User(int score, int age){  
  10.         super();  
  11.         this.score = score;  
  12.         this.age = age;  
  13.     }  
  14.   
  15.     public int getScore() {  
  16.         return score;  
  17.     }  
  18.   
  19.     public void setScore(int score) {  
  20.         this.score = score;  
  21.     }  
  22.   
  23.     public int getAge() {  
  24.         return age;  
  25.     }  
  26.   
  27.     public void setAge(int age) {  
  28.         this.age = age;  
  29.     }  
  30.   
  31.     @Override  
  32.     public int compareTo(User o) {  
  33.         int i = this.getAge() - o.getAge();//先按照年龄排序  
  34.         if(i == 0){  
  35.             return this.score - o.getScore();//如果年龄相等了再用分数进行排序  
  36.         }  
  37.         return i;  
  38.     }  
  39.       
  40. }  
  41.   
  42. public static void main(String[] args) {  
  43.         List users = new ArrayList();  
  44.         users.add(new User(7826));  
  45.         users.add(new User(6723));  
  46.         users.add(new User(3456));  
  47.         users.add(new User(5523));  
  48.         Collections.sort(users);  
  49.         for(User user : users){  
  50.             System.out.println(user.getScore() + ”,” + user.getAge());  
  51.         }  
  52. }  
package core.java.collection.collections;public class User implements Comparable{    private int score;    private int age;    public User(int score, int age){        super();        this.score = score;        this.age = age;    }    public int getScore() {        return score;    }    public void setScore(int score) {        this.score = score;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    @Override    public int compareTo(User o) {        int i = this.getAge() - o.getAge();//先按照年龄排序        if(i == 0){            return this.score - o.getScore();//如果年龄相等了再用分数进行排序        }        return i;    }}public static void main(String[] args) {        List users = new ArrayList();        users.add(new User(78, 26));        users.add(new User(67, 23));        users.add(new User(34, 56));        users.add(new User(55, 23));        Collections.sort(users);        for(User user : users){            System.out.println(user.getScore() + "," + user.getAge());        }}
输出结果:
55,23
67,23
78,26
34,56
我们会发现sort(List)方法中List中的T必须实现Comparable接口,然后实现
compareTo()方法,该方法的返回值0代表相等,1表示大于,-1表示小于;为什么
在简单例子中没有看到实现Comparable接口呢?是因为Integer类其实自己已经实现
了Comparable接口,Java已经给我们做好了。

Collections提供的第二种排序方法sort(List list, Comparator<? super T> c)
先看例子:
[java] view plain copy print ?
  1. package core.java.collection.collections;  
  2.   
  3. public class Students {  
  4.       
  5.     private int age;  
  6.     private int score;  
  7.       
  8.     public Students(int age, int score){  
  9.         super();  
  10.         this.age = age;  
  11.         this.score = score;  
  12.     }  
  13.       
  14.     public int getAge() {  
  15.         return age;  
  16.     }  
  17.     public void setAge(int age) {  
  18.         this.age = age;  
  19.     }  
  20.     public int getScore() {  
  21.         return score;  
  22.     }  
  23.     public void setScore(int score) {  
  24.         this.score = score;  
  25.     }  
  26. }  
  27. public static void main(String[] args) {  
  28.         List students = new ArrayList();  
  29.         students.add(new Students(23100));  
  30.         students.add(new Students(2798));  
  31.         students.add(new Students(2999));  
  32.         students.add(new Students(2998));  
  33.         students.add(new Students(2289));  
  34.         Collections.sort(students, new Comparator() {  
  35.   
  36.             @Override  
  37.             public int compare(Students o1, Students o2) {  
  38.                 int i = o1.getScore() - o2.getScore();  
  39.                 if(i == 0){  
  40.                     return o1.getAge() - o2.getAge();  
  41.                 }  
  42.                 return i;  
  43.             }  
  44.         });  
  45.         for(Students stu : students){  
  46.             System.out.println(”score:” + stu.getScore() + “:age” + stu.getAge());  
  47.         }  
  48. }  
package core.java.collection.collections;public class Students {    private int age;    private int score;    public Students(int age, int score){        super();        this.age = age;        this.score = score;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public int getScore() {        return score;    }    public void setScore(int score) {        this.score = score;    }}public static void main(String[] args) {        List students = new ArrayList();        students.add(new Students(23, 100));        students.add(new Students(27, 98));        students.add(new Students(29, 99));        students.add(new Students(29, 98));        students.add(new Students(22, 89));        Collections.sort(students, new Comparator() {            @Override            public int compare(Students o1, Students o2) {                int i = o1.getScore() - o2.getScore();                if(i == 0){                    return o1.getAge() - o2.getAge();                }                return i;            }        });        for(Students stu : students){            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接口,只是在sort()方法
中多传入一个参数,只不过该参数是一个接口我们需要实现其compare方法。

以上就是是Java中Colelctions工具类为我们提供的两种集合排序方法。


更多相关文章

  1. android Activity面试启动流程分析
  2. android--由文件名获取文件Id的两种方法
  3. 服务--Service
  4. android Launcher3中定制第三方apk图标,实现类似主题功能
  5. Android使用JSONObject和GSON方法解析JSON格式数据
  6. Android学习笔记_32_通过WebView实现JS代码与Java代码互相通信
  7. INSTALL_FAILED_CONFLICTING_PROVIDER错误解决方法
  8. Android类参考---Fragment(八)
  9. android xml解析 - sax

随机推荐

  1. Porting Android to S3C6410
  2. android下安装AnDroidDraw
  3. Android的自定义长按
  4. Android中Intent传值
  5. android之OkHttpClient通信
  6. Android中消息机制中一些细节知识点
  7. android中使用SQLite进行CRUD操作的实例
  8. android Environment 常用方法(获取存储目
  9. android 电子签名 手写签名 功能实现
  10. android 图片的浏览、缩放、拖动和自动居