Java8利用stream流实现数字排序和中文排序(包括升降序)

代码部分

利用stream的sorted()进行实现中文排序和数字排序(包括降序和升序),非常简单。

package com.chstar.elasticsearchdemo.utils;

import java.text.Collator;
import java.util.*;
import java.util.stream.Collectors;

public class Demo {
    static class Student {//只是为了方便演示

        private Integer id;
        private String name;
        private String grade;

        public String getGrade() {
            return grade;
        }

        public void setGrade(String grade) {
            this.grade = grade;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

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

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

        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", grade='" + grade + '\'' +
                    '}';
        }
    }
    public static void main(String[] args) {

        //测试数据,可以看到顺序是乱的。
        List<Student> user = new ArrayList<>();
        user.add( new Student(1, "张三","一年级"));
        user.add( new Student(4, "李四","三年级"));
        user.add( new Student(2, "王五","二年级"));
        user.add( new Student(3, "赵六","四年级"));

        //按编号升序(默认)
        List<Student> collect1 = user.stream().sorted(Comparator.comparingInt(Student::getId)).collect(Collectors.toList());
        //遍历输出
        System.out.println("编号升序");
        collect1.forEach(System.out::println);
        System.out.println("===========================================================================");

        //按年级升序(默认)
        List<Student> collect = user.stream()
                .sorted((o1, o2) -> Collator.getInstance(Locale.TRADITIONAL_CHINESE).compare(o1.getGrade(),o2.getGrade())).collect(Collectors.toList());
        //遍历输出
        System.out.println("中文升序");
        collect.forEach(System.out::println);
        System.out.println("===========================================================================");

        //按编号降序
        List<Student> collect2 = user.stream().sorted((o1, o2) -> o2.getId() - o1.getId()).collect(Collectors.toList());
        //遍历输出
        System.out.println("编号降序");
        collect2.forEach(System.out::println);
        System.out.println("===========================================================================");

        //按年级降序
        List<Student> collect3 =
                user.stream().sorted((o1, o2) -> Collator.getInstance(Locale.TRADITIONAL_CHINESE).compare(o2.getGrade(),o1.getGrade())).collect(Collectors.toList());
        //遍历输出
        System.out.println("中文降序");
        collect3.forEach(System.out::println);

    }
}

测试结果

可以看到成功:

stream确实好用一行代码搞定聚合、过滤、排序操作等。

posted @ 2021-03-31 12:53  _未来可期  阅读(5929)  评论(2编辑  收藏  举报