Java学习笔记程序员Android

ArrayList等常见集合的排序问题

2016-08-11  本文已影响133人  craneyuan

对于ArrayList等常用的集合具体业务类,基本上都实现了Comparable接口,即可以用来比较装载的对象实体。

主要用Collections.sort方法对集合类中的对象进行排序

Collections.sort的两种重载方法

Java集合框架:


comparator

代码示例(演示Collections.sort(list, comparator)方法):

注意:本代码均已在jdk1.6版本下通过测试

public class Student {
    private int id;
    private String name;
    private int age;

    /**
     * @Title: Student
     * @Description: TODO
     * @param:
     * @throws
     */
    public Student(int id, String name, int age) {
        // TODO Auto-generated constructor stub
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return String.format("Student [age=%s, name=%s, id=%s]", age, name, id);
    }
}
public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<Student> arrayList = new ArrayList<Student>();
        Student s1 = new Student(1, "jack", 20);
        Student s2 = new Student(2, "jack", 20);
        Student s3 = new Student(3, "lily", 29);
        Student s4 = new Student(4, "tom", 30);
        Student s5 = new Student(5, "rose", 31);
        Student s6 = new Student(6, "crane", 20);
        Student s7 = new Student(7, "jack", 25);
        Student s8 = new Student(8, "rose", 27);
        Student s9 = new Student(9, "lucy", 18);

        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        arrayList.add(s4);
        arrayList.add(s5);
        arrayList.add(s6);
        arrayList.add(s7);
        arrayList.add(s8);
        arrayList.add(s9);
        Comparator<Student> studentComparator = new Comparator<Student>() {

            /**
             * 
             * @Title: compare
             * @Description: 先比较age,再比较name,最后比较id
             * @param: @param o1
             * @param: @param o2
             * @param: @return
             * @return: int
             * @throws
             */
            @Override
            public int compare(Student o1, Student o2) {
                // TODO Auto-generated method stub
                if (o1.getAge() != o2.getAge()) {
                    return o1.getAge() - o2.getAge();
                } else if (!o1.getName().equals(o2.getName())) {
                    return o1.getName().compareTo(o2.getName());
                } else if (o1.getId() != o2.getId()) {
                    return o1.getId() - o2.getId();
                }
                return 0;
            }
        };
        Collections.sort(arrayList, studentComparator);

        for (Student student : arrayList) {
            System.out.println(student.toString());
        }
    }
Student [age=18, name=lucy, id=9]
Student [age=20, name=crane, id=6]
Student [age=20, name=jack, id=1]
Student [age=20, name=jack, id=2]
Student [age=25, name=jack, id=7]
Student [age=27, name=rose, id=8]
Student [age=29, name=lily, id=3]
Student [age=30, name=tom, id=4]
Student [age=31, name=rose, id=5]
上一篇下一篇

猜你喜欢

热点阅读