Java ArrayList的自定义排序

来源:互联网 发布:清除数据英文 编辑:程序博客网 时间:2024/06/05 09:46

Java中实现对list的自定义排序主要通过两种方式

1)让需要进行排序的对象的类实现Comparable接口,重写compareTo(T o)方法,在其中定义排序规则,那么就可以直接调用Collections.sort()来排序对象数组

public class Student implements Comparable{    private int id;    private int age;    private int height;    private String name;    public Student(int id, String name, int age, int height) {        this.id = id;        this.name = name;        this.age = age;        this.height = height;    }    public int getId() {        return id;    }    public int getAge() {        return age;    }    public int getHeight() {        return height;    }    public String getName() {        return name;    }    public void setId(int id) {        this.id = id;    }    public void setAge(int age) {        this.age = age;    }    public void setName(String name) {        this.name = name;    }    public void setHeight(int height) {        this.height = height;    }    @Override    public int compareTo(Object o) {        Student s = (Student) o;        if (this.age > s.age) {            return 1;        }        else if (this.age < s.age) {            return -1;        }        else {            if (this.height >= s.height) {                return 1;            }            else {                return -1;            }        }    }}

测试类:

import java.util.*;public class Test {    public static void printData(List<Student> list) {        for (Student student : list) {            System.out.println("学号:" + student.getId() + " 姓名:" + student.getName() + " 年龄" + student.getAge() + " 身高:" + student.getHeight());        }    }    public static void main(String[] args) {        List<Student> list = new ArrayList<>();        list.add(new Student(1, "A", 20, 180));        list.add(new Student(2, "B", 21, 175));        list.add(new Student(3, "C", 22, 190));        list.add(new Student(4, "D", 21, 170));        list.add(new Student(5, "E", 20, 185));        System.out.println("before sorted");        printData(list);        Collections.sort(list);        System.out.println("after age and height sorted");        printData(list);    }}

结果:

before sorted学号:1 姓名:A 年龄20 身高:180学号:2 姓名:B 年龄21 身高:175学号:3 姓名:C 年龄22 身高:190学号:4 姓名:D 年龄21 身高:170学号:5 姓名:E 年龄20 身高:185after age and height sorted学号:1 姓名:A 年龄20 身高:180学号:5 姓名:E 年龄20 身高:185学号:4 姓名:D 年龄21 身高:170学号:2 姓名:B 年龄21 身高:175学号:3 姓名:C 年龄22 身高:190

2)实现比较器接口Comparator,重写compare方法,直接当做参数传进sort中

public class Student {    private int id;    private int age;    private int height;    private String name;    public Student(int id, String name, int age, int height) {        this.id = id;        this.name = name;        this.age = age;        this.height = height;    }    public int getId() {        return id;    }    public int getAge() {        return age;    }    public int getHeight() {        return height;    }    public String getName() {        return name;    }    public void setId(int id) {        this.id = id;    }    public void setAge(int age) {        this.age = age;    }    public void setName(String name) {        this.name = name;    }    public void setHeight(int height) {        this.height = height;    }}

测试类:

import java.util.*;public class Test {    public static void printData(List<Student> list) {        for (Student student : list) {            System.out.println("学号:" + student.getId() + " 姓名:" + student.getName() + " 年龄" + student.getAge() + " 身高:" + student.getHeight());        }    }    public static void main(String[] args) {        List<Student> list = new ArrayList<>();        list.add(new Student(1, "A", 20, 180));        list.add(new Student(2, "B", 21, 175));        list.add(new Student(3, "C", 22, 190));        list.add(new Student(4, "D", 21, 170));        list.add(new Student(5, "E", 20, 185));        System.out.println("before sorted");        printData(list);        Collections.sort(list, new Comparator<Student>() {            @Override            public int compare(Student o1, Student o2) {                if(o1.getAge() >= o2.getAge()) {                    return 1;                }                else {                    return -1;                }            }        });        System.out.println("after age sorted");        printData(list);        Collections.sort(list, new Comparator<Student>() {            @Override            public int compare(Student o1, Student o2) {                if(o1.getAge() > o2.getAge()) {                    return 1;                }                else if (o1.getAge() < o2.getAge()){                    return -1;                }                else {                    if (o1.getHeight() >= o2.getHeight()) {                        return 1;                    }                    else {                        return -1;                    }                }            }        });        System.out.println("after age and height sorted");        printData(list);    }}

输出结果:

before sorted学号:1 姓名:A 年龄20 身高:180学号:2 姓名:B 年龄21 身高:175学号:3 姓名:C 年龄22 身高:190学号:4 姓名:D 年龄21 身高:170学号:5 姓名:E 年龄20 身高:185after age sorted学号:1 姓名:A 年龄20 身高:180学号:5 姓名:E 年龄20 身高:185学号:2 姓名:B 年龄21 身高:175学号:4 姓名:D 年龄21 身高:170学号:3 姓名:C 年龄22 身高:190after age and height sorted学号:1 姓名:A 年龄20 身高:180学号:5 姓名:E 年龄20 身高:185学号:4 姓名:D 年龄21 身高:170学号:2 姓名:B 年龄21 身高:175学号:3 姓名:C 年龄22 身高:190

单从上面的例子可以看出排序是稳定的,去看了下java的Collections.sort的源代码,确实是基于稳定的归并排序实现的,内部还做了优化,叫TimSort


0 0
原创粉丝点击