第十四节 Java8 stream流之分组 groupingBy 的使用

亮子 2022-08-01 11:48:35 17319 0 0 0

1、示例代码

package com.shenmazong.zg6;

import com.alibaba.fastjson2.JSON;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2022/8/1 19:20
 */

public class Student {

    //班级
    private String classNumber;
    //姓名
    private String name;
    //年龄
    private int age;
    //地址
    private String address;
    //数学成绩
    private int mathScores;
    //语文成绩
    private int chainessScores;

    public Student(String classNumber, String name, int age, String address, int mathScores, int chainessScores) {
        this.classNumber = classNumber;
        this.name = name;
        this.age = age;
        this.address = address;
        this.mathScores = mathScores;
        this.chainessScores = chainessScores;
    }

    public String getClassNumber() {
        return classNumber;
    }

    public void setClassNumber(String classNumber) {
        this.classNumber = classNumber;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getMathScores() {
        return mathScores;
    }

    public void setMathScores(int mathScores) {
        this.mathScores = mathScores;
    }

    public int getChainessScores() {
        return chainessScores;
    }

    public void setChainessScores(int chainessScores) {
        this.chainessScores = chainessScores;
    }

    //https://blog.csdn.net/sugelachao/article/details/117736155

    public static void main(String[] args) {
        //--1 添加数据
        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());

        //--2 按照班级分组
        Map<String, List<Student>> collect1 = students.stream().collect(Collectors.groupingBy(Student::getClassNumber));
        System.out.println(JSON.toJSONString(collect1));

        //--3 按照班级分组得到每个班级的同学姓名
        Map<String, List<String>> collect2 = students.stream().collect(
                Collectors.groupingBy(Student::getClassNumber, Collectors.mapping(Student::getName, Collectors.toList()))
        );
        System.out.println(JSON.toJSONString(collect2));

        //--4 统计每个班级人数
        Map<String, Long> collect3 = students.stream().collect(
                Collectors.groupingBy(Student::getClassNumber, Collectors.counting())
        );
        System.out.println(JSON.toJSONString(collect3));

        //--5 求每个班级的数学平均成绩
        Map<String, Double> collect4 = students.stream().collect(
                Collectors.groupingBy(Student::getClassNumber, Collectors.averagingDouble(Student::getMathScores))
        );
        System.out.println(JSON.toJSONString(collect4));

        //--6 按班级分组求每个同学的总成绩
        Map<String, Map<String, Integer>> collect5 = students.stream().collect(
                Collectors.groupingBy(
                        Student::getClassNumber,
                        Collectors.toMap(Student::getName, student -> student.getMathScores() + student.getChainessScores())
                )
        );
        System.out.println(JSON.toJSONString(collect5));

        //--7 嵌套分组,先按班级分组,再按年龄分组
        Map<String, Map<Integer, List<Student>>> collect6 = students.stream().collect(
                Collectors.groupingBy(Student::getClassNumber, Collectors.groupingBy(Student::getAge))
        );
        System.out.println(JSON.toJSONString(collect6));

        //--8 分组后得到一个线程安全的ConcurrentMap
        ConcurrentMap<String, List<Student>> collect7 = students.stream().collect(
                Collectors.groupingByConcurrent(Student::getClassNumber)
        );
        System.out.println(JSON.toJSONString(collect7));

        //--9 根据年龄分组并小到大排序,TreeMap默认为按照key升序
        TreeMap<Integer, List<String>> collect8 = students.stream().collect(
                Collectors.groupingBy(Student::getAge, TreeMap::new, Collectors.mapping(Student::getName, Collectors.toList()))
        );
        System.out.println(JSON.toJSONString(collect8));
        //{16:["张三","王五","赵六"],17:["李四","老八"],18:["钱七"]}

        //--10 根据年龄分组并大到小排序,因为TreeMap默认为按照key升序,所以排完顺序再反转一下就OK了
        TreeMap<Integer, List<String>> collect9 = students.stream().collect(
                Collectors.groupingBy(Student::getAge, TreeMap::new, Collectors.mapping(Student::getName, Collectors.toList()))
        );
        Map<Integer, List<String>> collect10 = collect9.descendingMap();
        System.out.println(JSON.toJSONString(collect10));
        //{18:["钱七"],17:["李四","老八"],16:["张三","王五","赵六"]}
    }
}

2、运行后的结果

{"700":[{"address":"北京","age":17,"chainessScores":90,"classNumber":"700","mathScores":78,"name":"李四"},{"address":"","age":18,"chainessScores":90,"classNumber":"700","mathScores":78,"name":"钱七"}],"701":[{"address":"北京","age":16,"chainessScores":90,"classNumber":"701","mathScores":78,"name":"张三"},{"address":"上海","age":16,"chainessScores":90,"classNumber":"701","mathScores":78,"name":"赵六"},{"address":"","age":17,"chainessScores":90,"classNumber":"701","mathScores":78,"name":"老八"}],"703":[{"address":"上海","age":16,"chainessScores":90,"classNumber":"703","mathScores":78,"name":"王五"}]}
{"700":["李四","钱七"],"701":["张三","赵六","老八"],"703":["王五"]}
{"700":2,"701":3,"703":1}
{"700":78.0,"701":78.0,"703":78.0}
{"700":{"钱七":168,"李四":168},"701":{"张三":168,"老八":168,"赵六":168},"703":{"王五":168}}
{"700":{"17":[{"address":"北京","age":17,"chainessScores":90,"classNumber":"700","mathScores":78,"name":"李四"}],"18":[{"address":"","age":18,"chainessScores":90,"classNumber":"700","mathScores":78,"name":"钱七"}]},"701":{"16":[{"address":"北京","age":16,"chainessScores":90,"classNumber":"701","mathScores":78,"name":"张三"},{"address":"上海","age":16,"chainessScores":90,"classNumber":"701","mathScores":78,"name":"赵六"}],"17":[{"address":"","age":17,"chainessScores":90,"classNumber":"701","mathScores":78,"name":"老八"}]},"703":{"16":[{"address":"上海","age":16,"chainessScores":90,"classNumber":"703","mathScores":78,"name":"王五"}]}}
{"700":[{"address":"北京","age":17,"chainessScores":90,"classNumber":"700","mathScores":78,"name":"李四"},{"address":"","age":18,"chainessScores":90,"classNumber":"700","mathScores":78,"name":"钱七"}],"701":[{"address":"北京","age":16,"chainessScores":90,"classNumber":"701","mathScores":78,"name":"张三"},{"address":"上海","age":16,"chainessScores":90,"classNumber":"701","mathScores":78,"name":"赵六"},{"address":"","age":17,"chainessScores":90,"classNumber":"701","mathScores":78,"name":"老八"}],"703":[{"address":"上海","age":16,"chainessScores":90,"classNumber":"703","mathScores":78,"name":"王五"}]}
{"16":["张三","王五","赵六"],"17":["李四","老八"],"18":["钱七"]}
{"18":["钱七"],"17":["李四","老八"],"16":["张三","王五","赵六"]}

参考文章