Java8 stream流之分组 groupingBy 的使用

众所周知,使用stream流可以让我们的代码看上去很简洁,现在我们实战使用一下stream的分组与分区。

准备用到的数据类
public class Student{
        //年级
        private String grade;
        //班级
        private String classNumber;
        //姓名
        private String name;
        //年龄
        private int age;
        //地址
        private String address;
        //数学成绩
        private int mathScores;
        //语文成绩
        private int chainessScores;
    }
添加数据
        Student student1 = new Student("701","张三",16,"北京",78,90);
        Student student2 = new Student("700","李四",17,"北京",78,90);
        Student student3 = new Student("703","王五",16,"上海",78,90);
        Student student4 = new Student("701","赵六",16,"上海",78,90);
        Student student5 = new Student("700","钱七",18,"",78,90);
        Student student6 = new Student("701","老八",17,"",78,90);
//这是一个高二年级的成绩单
        List<Student> students = Stream.of(student1,student2,student3,student4,student5,student6).collect(Collectors.toList());

按照班级分组

Map<String, List<Student>> collect = students.stream().collect(Collectors.groupingBy(Student::getClassNumber));
System.out.println(JSON.toJSONString(collect));
//{"700":[{"age":17,"chainessScores":90,"classNumber":"700","mathScores":78,"name":"李四"},{"age":18,"chainessScores":90,"classNumber":"700","mathScores":78,"name":"钱七"}],
//"701":[{"age":16,"chainessScores":90,"classNumber":"701","mathScores":78,"name":"张三"},{"age":16,"chainessScores":90,"classNumber":"701","mathScores":78,"name":"赵六"},{"age":17,"chainessScores":90,"classNumber":"701","mathScores":78,"name":"老八"}],
//"703":[{"age":16,"chainessScores":90,"classNumber":"703","mathScores":78,"name":"王五"}]}

按照班级分组得到每个班级的同学姓名

Map<String, List<String>> collect = students.stream().collect(Collectors.groupingBy(Student::getClassNumber, Collectors.mapping(Student::getName, Collectors.toList())));
System.out.println(JSON.toJSONString(collect));
//{"700":["李四","钱七"],"701":["张三","赵六","老八"],"703":["王五"]}

统计每个班级人数

Map<String, Long> collect = students.stream().collect(Collectors.groupingBy(Student::getClassNumber, Collectors.counting()));
System.out.println(JSON.toJSONString(collect));
//{"700":2,"701":3,"703":1}        

求每个班级的数学平均成绩

Map<String, Double> collect = students.stream().collect(Collectors.groupingBy(Student::getClassNumber, Collectors.averagingDouble(Student::getMathScores)));
System.out.println(JSON.toJSONString(collect));
//{"700":65.0,"701":61.0,"703":82.0}

按班级分组求每个同学的总成绩

Map<String, Map<String, Integer>> collect = students.stream().collect(Collectors.groupingBy(Student::getClassNumber, Collectors.toMap(Student::getName, student -> student.getMathScores() + student.getChainessScores())));
System.out.println(JSON.toJSONString(collect));
//{"700":{"钱七":150,"李四":160},"701":{"张三":168,"老八":148,"赵六":137},"703":{"王五":172}}

嵌套分组,先按班级分组,再按年龄分组

Map<String, Map<Integer, List<Student>>> collect = students.stream().collect(Collectors.groupingBy(Student::getClassNumber, Collectors.groupingBy(Student::getAge)));

分组后得到一个线程安全的ConcurrentMap

ConcurrentMap<String, List<Student>> collect = students.stream().collect(Collectors.groupingByConcurrent(Student::getClassNumber));

加上排序来一波
根据年龄分组并小到大排序,TreeMap默认为按照key升序

TreeMap<Integer, List<String>> collect = students.stream().collect(Collectors.groupingBy(Student::getAge, TreeMap::new, Collectors.mapping(Student::getName, Collectors.toList())));
System.out.println(JSON.toJSONString(collect));
 //{16:["张三","王五","赵六"],17:["李四","老八"],18:["钱七"]}

根据年龄分组并大到小排序,因为TreeMap默认为按照key升序,所以排完顺序再反转一下就OK了

TreeMap<Integer, List<String>> collect = students.stream().collect(Collectors.groupingBy(Student::getAge, TreeMap::new, Collectors.mapping(Student::getName, Collectors.toList())));
Map<Integer, List<String>> collect2 = collect.descendingMap();
System.out.println(JSON.toJSONString(collect2));
//{18:["钱七"],17:["李四","老八"],16:["张三","王五","赵六"]}

你可能感兴趣的:(java,后端)