技术交流


JAVA——对象数组排序

Apr 1, 2019 9:10:46 PM
221
1

Comparable接口

Arrays类中的sort方法会对对象数组进行排序,但前提是对象所属的类必须实现了Comparable接口。

例:使用Comparable接口对学生对象数组排序(按学生年龄排序)

public class ComparableStudent {
    public static void main(String[] args) {
        //构造三个学生对象放入数组
        Student student1 = new Student("小明",12);
        Student student2 = new Student("小红",11);
        Student student3 = new Student("小刚",14);
        Student[] students = {student1,student2,student3};
        System.out.println("排序前:\n" + Arrays.toString(students));

        //排序
        Arrays.sort(students);
        System.out.println("排序后:\n" + Arrays.toString(students));
    }

}

class Student implements Comparable<Student> {

    private String name;

    private Integer age;

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return Integer.compare(age, o.age);
    }
}

打印结果:

排序前:

[Student{name='小明', age=12}, Student{name='小红', age=11}, Student{name='小刚', age=14}]

排序后:

[Student{name='小红', age=11}, Student{name='小明', age=12}, Student{name='小刚', age=14}]

Comparator接口

Arrays.sort()还有第二种用法,就是一个数组和比较器作为参数,比较器必须实现Comparator接口。

例:使用Comparator接口对学生对象数组排序(按学生年龄排序)

public class ComparatorStudent {
    public static void main(String[] args) {
        //构造三个学生对象放入数组
        Student1 student1 = new Student1("小明", 12);
        Student1 student2 = new Student1("小红", 11);
        Student1 student3 = new Student1("小刚", 14);
        Student1[] students = {student1, student2, student3};
        System.out.println("排序前:\n" + Arrays.toString(students));

        //排序
        Arrays.sort(students, new AgeComparator());
        System.out.println("排序后:\n" + Arrays.toString(students));
    }

}

/**
 * 学生类
 */
class Student1 {

    private String name;

    private Integer age;


    public Student1(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student1{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }
}

/**
 * 比较器
 */
class AgeComparator implements Comparator<Student1> {

    @Override
    public int compare(Student1 o1, Student1 o2) {
        return o1.getAge() - o2.getAge();
    }
}

打印结果:

排序前:

[Student1{name='小明', age=12}, Student1{name='小红', age=11}, Student1{name='小刚', age=14}]

排序后:

[Student1{name='小红', age=11}, Student1{name='小明', age=12}, Student1{name='小刚', age=14}]

lambda表达式

java8lambda表达式可以简单快速地实现用比较器的方式对对象数组排序。

例:使用lambda表达式接口对学生对象数组排序(按学生年龄排序)

public class LambdaStudent {
    public static void main(String[] args) {
        //构造三个学生对象放入数组
        Student2 student1 = new Student2("小明", 12);
        Student2 student2 = new Student2("小红", 11);
        Student2 student3 = new Student2("小刚", 14);
        Student2[] students = {student1, student2, student3};
        System.out.println("排序前:\n" + Arrays.toString(students));

        //排序
        Arrays.sort(students, (s1, s2) -> s1.getAge() - s2.getAge());
        System.out.println("排序后:\n" + Arrays.toString(students));
    }

}

class Student2 {

    private String name;

    private Integer age;

    public Student2(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student2{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }
}

打印结果:

排序前:

[Student1{name='小明', age=12}, Student1{name='小红', age=11}, Student1{name='小刚', age=14}]

排序后:

[Student1{name='小红', age=11}, Student1{name='小明', age=12}, Student1{name='小刚', age=14}]


如果你喜欢我的内容,就请打赏一下吧
微信
支付宝
温馨提示: 你的打赏金额会直接转入对方账户,不可退回。

评论专区


审核通过的评论(0)
暂无评论信息
个人名片

  欢迎来到“浩瀚星尘”的个人博客!
  首先,该博客用于分享本人的生活事迹与兴趣爱好; 此外,该博客的主要作用便是与广大的小伙伴一起分享探讨开发技术, 希望大家多多关照。

网名: 浩瀚星尘
城市: 重庆
工作: java