zoukankan      html  css  js  c++  java
  • Java List集合排序的两种方法

    1.使用 Collections 工具类中的 sort() 方法

      参数不同: void sort(List list) 在自定义类User里面实现Comparable<User>接口,并重写抽象方法compareTo(Student o);
            void sort(List list, Comparator c) 第二个参数为了省事,可以直接使用匿名内部类

    public class User implements Comparable<User>{  
          
        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<User> users = new ArrayList<User>();  
            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());  
            }  
    }
    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> students = new ArrayList<Students>();  
            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<Students>() {  
      
                @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());  
            }  
    }

    2.直接使用list.sort()方法,传入实现Comparator接口的实现类的实例,为了省事直接传入匿名内部类

    public class Students {
    
        private int age;
        private int score;
    
        public Students(int age, int score){
            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> students = new ArrayList<Students>();   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));   students.sort(new Comparator<Students>() {     @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());   } }
  • 相关阅读:
    python2.7之打飞机(文末附素材链接)
    python画小猪佩奇
    什么叫递归
    DIV居中的几种方法
    什么是控制反转
    上传图片及时预览
    MVC与三层的区别
    From表单提交刷新页面?
    文件上传之form表单篇
    文件上传之伪Ajax篇
  • 原文地址:https://www.cnblogs.com/roadlandscape/p/12093907.html
Copyright © 2011-2022 走看看