工作中遇到的困难。

1.需要对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方法,否则可能出现怪异的表现
View Code

 

posted @ 2019-06-21 10:45  宥宥美美  阅读(269)  评论(0编辑  收藏  举报