newcomparator:从多个方面详解它的使用

发布时间:2023-05-19

一、简介

<T> Comparator<T>

是一个函数接口,用于比较两个对象。使用它可根据需要确定按升序或降序排列。在Java API中,这个接口是一个功能强大的方式,可以用于排序、搜索、treeSet和PriorityQueue。而为了更好的排序功能,java新的comparator中新增了一个新的允许使用Lambda表达式来代替普通方法的排序规则接口:newcomparator

二、newcomparator与comparator的区别

newcomparator可以用来定义基于lambda表达式的排序规则。而对于原有的comparator,则必须用定义一个类并实现comparable接口,这样在类中便可以对需要排序的元素进行自定义的排序规则。

三、newcomparator的使用方法

在使用newcomparator时,需要根据排序规则建立lambda表达式并作为newcomparator的参数。例如下面这个例子演示怎样使用newcomparator来对一个字符串列表进行不区分大小写的排序。

public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    list.add("apple");
    list.add("banana");
    list.add("orange");
    list.add("Pear");
    list.sort(String.CASE_INSENSITIVE_ORDER);
    list.forEach(System.out::println);
}

四、newcomparator和stream的配合使用

Java 8的新特性增加了stream api。stream api不仅扩展了对集合的操作,也很好的与newcomparator配合使用。通过对字段进行过滤、排序和分组,可以快速生成自定义的聚合数据。

public class Student {
    private String name;
    private int score;
    public Student(String name, int score) {
        super();
        this.name = name;
        this.score = score;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
}
public class NewComparatorDemo {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student("Tom", 90));
        list.add(new Student("Jerry", 80));
        list.add(new Student("Tim", 85));
        list.add(new Student("Sunny", 89));
        list.add(new Student("Mary", 95));
        // 按分数排序
        list.stream().sorted(Comparator.comparing(Student::getScore)).forEach(System.out::println);
        // 输出结果:
        // Jerry 80
        // Tim 85
        // Sunny 89
        // Tom 90
        // Mary 95
        // 按名字排序
        list.stream().sorted(Comparator.comparing(Student::getName)).forEach(System.out::println);
        // 输出结果:
        // Jerry 80
        // Mary 95
        // Sunny 89
        // Tim 85
        // Tom 90
    }
}

五、数据类型的排序

对于基本数据类型,可以直接使用以下的写法

int[] nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
Arrays.stream(nums).boxed().sorted(Comparator.reverseOrder())
    .forEach(System.out::println);
// 输出结果:
// 9
// 6
// 5
// 5
// 5
// 4
// 3
// 3
// 2
// 1
// 1

而对于对象类型,需要根据字段进行排序。下面的例子演示了怎样用数组排序来实现对自定义对象集合的排序。

public class Employee {
    private String name;
    private Integer age;
    private Double salary;
    // 省略get/set方法
    public static void main(String[] args) {
        Employee[] employees = new Employee[4];
        employees[0] = new Employee("Tom", 23, 5000.0);
        employees[1] = new Employee("Jerry", 21, 4500.0);
        employees[2] = new Employee("Sunny", 25, 5500.0);
        employees[3] = new Employee("Marry", 23, 6500.0);
        Arrays.sort(employees, Comparator.comparing(Employee::getAge).reversed());
        System.out.println("按年龄降序排序的结果:");
        Arrays.stream(employees).forEach(System.out::println);
        // 输出结果:
        // Marry 23 6500.0
        // Tom 23 5000.0
        // Sunny 25 5500.0
        // Jerry 21 4500.0
        Arrays.sort(employees, Comparator.comparingDouble(Employee::getSalary));
        System.out.println("按工资升序排序的结果:");
        Arrays.stream(employees).forEach(System.out::println);
        // 输出结果:
        // Jerry 21 4500.0
        // Tom 23 5000.0
        // Sunny 25 5500.0
        // Marry 23 6500.0
    }
}