• 对List里的对象元素进行排序


    public class Student {
        private int studentId;
        private String studentName;
        private int age;
        public Student(int studentId , String studentName, int age){
            this.studentId=studentId;
            this.studentName=studentName;
            this.age=age;
        }
        public int getStudentId() {
            return studentId;
        }
        public void setStudentId(int studentId) {
            this.studentId = studentId;
        }
        public String getStudentName() {
            return studentName;
        }
        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        
    }
    
    
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    
    public class test {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            Comparator<Student> comparator = new Comparator<Student>() {
                public int compare(Student s1, Student s2) {
                    // 先排年龄
                    if (s1.getAge() != s2.getAge()) {
                        return s1.getAge() - s2.getAge();
                    } else if (!s1.getStudentName().equals(s2.getStudentName())) {
                        // 年龄相同则按姓名排序
                        return s1.getStudentName().compareTo(s2.getStudentName());
                    } else {
                        // 姓名也相同则按学号排序
                        return s1.getStudentId() - s2.getStudentId();
                    }
                }
            };
             Student stu1 = new Student (1,"zhangsan",28);
             Student stu2 = new Student (2,"zhagnsan",19);
             Student stu3 = new Student (3,"wangwu",19);
             Student stu4 = new Student (4,"wangwu",19);
             Student stu5 = new Student (5,"zhaoliu",18);
    
              ArrayList<Student> list = new ArrayList<Student>();
              list.add(stu1);
              list.add(stu2);
              list.add(stu3);
              list.add(stu4);
              list.add(stu5);
              //这里就会自动根据规则进行排序
              Collections.sort(list,comparator);
              for(int i=0;i<list.size();i++){
                  Student stu=list.get(i);
                  System.out.println("年龄:"+stu.getAge()+"   姓名:"+stu.getStudentName()+"   学号:"+stu.getStudentId());
              }
    
        }
    
    }
    
    第二个是实现Comparable接口
    
    public class Student2 implements Comparable<Student2>{  //必须实现CompareTo()
        private int studentId;
        private String studentName;
        private int age;
        public Student2(int studentId , String studentName, int age){
            this.studentId=studentId;
            this.studentName=studentName;
            this.age=age;
        }
        public int getStudentId() {
            return studentId;
        }
        public void setStudentId(int studentId) {
            this.studentId = studentId;
        }
        public String getStudentName() {
            return studentName;
        }
        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public int compareTo(Student2 o) {
            if(age!=o.getAge()){
                return age-o.getAge();
            }else if(!studentName.equals(o.getStudentName())){
                return studentName.compareTo(o.getStudentName());
            }else {
                return studentId-o.getStudentId();
            }
        }
        @Override
        public boolean equals(Object obj) {
            if(obj instanceof Student2){
                Student2 stu=(Student2)obj;
                if((age==stu.getAge())&&(studentName.equals(stu.getStudentName()))&&(studentId==stu.getStudentId())){
                    return true;
                }else
                    return true;
            }else{
                return false;
            }
        }
        
        
    }
    
    
    
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    
    public class test2 {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            Student2 stu1 = new Student2 (1,"zhangsan",28);
            Student2 stu2 = new Student2 (2,"zhagnsan",19);
            Student2 stu3 = new Student2 (3,"wangwu",19);
            Student2 stu4 = new Student2 (4,"wangwu",19);
            Student2 stu5 = new Student2 (5,"zhaoliu",18);
    
              ArrayList<Student2> list = new ArrayList<Student2>();
              list.add(stu1);
              list.add(stu2);
              list.add(stu3);
              list.add(stu4);
              list.add(stu5);
              //这里就会自动根据规则进行排序
              Collections.sort(list);
              for(int i=0;i<list.size();i++){
                  Student2 stu=list.get(i);
                  System.out.println("年龄:"+stu.getAge()+"   姓名:"+stu.getStudentName()+"   学号:"+stu.getStudentId());
              }
    
        }
    
    }
    
    结果:
    
    年龄:18   姓名:zhaoliu   学号:5
    
    年龄:19   姓名:wangwu   学号:3
    
    年龄:19   姓名:wangwu   学号:4
    
    年龄:19   姓名:zhagnsan   学号:2
    
    年龄:28   姓名:zhangsan   学号:1
    
    注:大家可以看下api文档里对接口 Comparable<T>接口的介绍,里面说是建议重写equals方法,否则可能出现怪异的表现
  • 相关阅读:
    作为管理者的基本职责
    websocket接口自动化的封装
    locust性能测试的使用
    git的协作提交流程
    关于接口自动化的实施步骤
    K8S的组件梳理
    jenkins pipeline中,失败后获取异常不中断业务
    pipline在执行的docker镜像中添加hosts
    sonar搭建
    django
  • 原文地址:https://www.cnblogs.com/fthjane/p/4797159.html
Copyright © 2020-2023  润新知