Comparator 排序

1.自定义排序器

// Comparator.comparing(list, Comparator);
// list 传入要排序的参数列表,Comparator 来指定一个排序器,这里可以用匿名内部类来实现一个自定义排序规则。

public static void main(String[] args) {
        Collator instance = Collator.getInstance(Locale.CHINA);
        instance.setStrength(Collator.PRIMARY);
        List<Employee> tempList = list.stream().sorted(Comparator.comparing(Employee::getName, instance)).collect(Collectors.toList());
        for (int i = 0; i < tempList.size(); i++) {
            System.out.println(tempList.get(i));
        }
    }

    private void test1() {
        List<Employee> collect = list.stream().sorted(Comparator.comparing(Employee::getGrade, (s1,s2)->{
                            if ("5".equals(s1)) {
                                return -1;
                            } else {
                                return 0;
                            }
                        })
// 这个排序和下面的排序效果完全不一样。
//                .thenComparing(Employee::getName, Comparator.reverseOrder())
//                .thenComparing(Employee::getName).reversed()
                )
                .collect(Collectors.toList());
        for (Employee employee : collect) {
            System.out.println(employee);
        }
    }

    static final List<Employee> list = Arrays.asList(
        new Employee("张三", "14", "6"),
        new Employee("李四", "16", "3"),
        new Employee("王子", "16", "3"),
        new Employee("王五", "12", "5")
    );

    static class Employee {
// get set方法省略了
        String name;
        String age;
        String grade;

        public Employee(String name, String age, String grade) {
            this.name = name;
            this.age = age;
            this.grade = grade;
        }
    }

2.中文首字母排序

Collator instance = Collator.getInstance(Locale.CHINA);
instance.setStrength(Collator.PRIMARY);
List<Employee> tempList = list.stream().sorted(Comparator.comparing(Employee::getName, instance)).collect(Collectors.toList());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 8 中的 Comparator 接口提供了很多新的方法,包括 reversed()、thenComparing()、nullsFirst() 和 nullsLast() 等方法,可以更方便地实现自定义排序。 下面是一个使用 Comparator 接口的示例,对一个列表中的 Person 对象进行排序: ```java import java.util.ArrayList; import java.util.Comparator; import java.util.List; public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public static void main(String[] args) { List<Person> persons = new ArrayList<>(); persons.add(new Person("Alice", 25)); persons.add(new Person("Bob", 20)); persons.add(new Person("Charlie", 30)); // 使用 Comparator 接口的默认方法进行排序 Comparator<Person> comparator = Comparator.comparing(Person::getAge); persons.sort(comparator); // 使用 Comparator 接口的 reversed() 方法进行倒序排序 comparator = comparator.reversed(); persons.sort(comparator); // 使用 Comparator 接口的 thenComparing() 方法进行多级排序 comparator = Comparator.comparing(Person::getAge) .thenComparing(Person::getName); persons.sort(comparator); // 使用 Comparator 接口的 nullsFirst() 和 nullsLast() 方法处理 null 值 persons.add(null); comparator = Comparator.nullsFirst(Comparator.comparing(Person::getName)); persons.sort(comparator); } } ``` 在上面的示例中,我们首先定义了一个 Person 类,然后创建了一个包含多个 Person 对象的列表。接下来,我们使用 Comparator 接口的默认方法 `comparing()`,按照年龄对 Person 对象进行排序,然后通过 `reversed()` 方法对排序结果进行倒序处理。接着,我们使用 `thenComparing()` 方法对排序结果进行多级排序,先按照年龄排序,再按照姓名排序。最后,我们使用 `nullsFirst()` 和 `nullsLast()` 方法处理空值,将空值放在排序结果的最前面或最后面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值