Java8 stream流之分组 groupingBy 的使用

Java8 stream流之分组 groupingBy 的使用使用stream流可以让我们的代码看上去很简洁,本文举例了按照班级分组、分组再过滤、统计分组后人数、嵌套分组、分组排序…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

众所周知,使用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:["张三","王五","赵六"]}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/171397.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Microsoft SQL Server 2008 R2出现索引超出数组界限

    Microsoft SQL Server 2008 R2出现索引超出数组界限目录问题 在创建关系图出现了弹窗 索引超出数组界限 网上看了很多文章 记录下解决方法 打微软的 SP3 补丁即可问题 在创建关系图出现了弹窗 索引超出数组界限 网上看了很多文章 记录下解决方法 打微软的 SP3 补丁即可跳转页面连接 微软 SP3 下载页面

    2025年9月7日
    4
  • vue父子组件传值 简单了解vuex

    vue父子组件传值 简单了解vuex一、vue的父子组件之间是如何传值的?首先呢,需要说说的是,vue既然有双向绑定,那为何会有父子组件之间的传值问题?这个问题也简单,vue的组件会供其他的vue页面进行调用,如果数组都是双向绑定的话,那么就容易混乱了,比如a,b页面绑了一个num=10,那b,c页面又绑了num=5,那vue实例的num到底听谁的?所以,这就是vue官网为什么说组件之间的数据只能是单项流通的,而且由父组件传递给…

    2022年5月16日
    93
  • IDEA2021.1 安装教程

    IDEA2021.1 安装教程工欲善其事必先利其器。一、下载IDEA官方下载地址:https://www.jetbrains.com/zh-cn/idea/download/二、安装IDEA注:安装IDEA之前需要我们机器上有JDK环境!!!双击打开安装即可:三、IDEA快速入门双击打开IntelliJIDEA2021.1.1×64:1、使用IDEA创建一个普通javase项目。2、使用IDEA创建一个maven项目。.

    2022年6月15日
    169
  • python字典dict方法_python中dict的用法

    python字典dict方法_python中dict的用法文章目录:一.字典(dict)的概念:二.字典(dict)的定义:1.一般格式:2.空字典:3.举例:注意:三.字典(dict)的一些基本操作:1.增:2.删:3.查:4.改:四.字典(dict)的常见操作:五.字典三种取值方式:1.value=字典名[key]:2.setdefault:3.get:六.字典的遍历:1.key:2.value:3.item:4.依次打印key和value:5.元素值和对应的下标索引(enumerate()):一.字典(dict)的概念:Python字典是另一种可变容器模

    2022年10月6日
    2
  • backtrack3(BT3) usb版 激活成功教程WIFI无线网络密码详细步骤

    backtrack3(BT3) usb版 激活成功教程WIFI无线网络密码详细步骤好像很多朋友都在找这东西,我来发上来吧!软件BAIDU一下就有得下了!一、先开始制作启动U盘:将下载好的backtrack3(BT3)usb版(下载地址在文章末尾)文件bt3b141207.rar直接解压到U盘(1G以上容量)。把Boot和BT3两个文件夹放到U盘的根目录,打开BOOT文件夹双击运行”bootinst.bat”批处理文件。U盘里就会多出4个文件.”isolinux.b…

    2022年10月1日
    5
  • 八叉树的相关内容

    八叉树的相关内容根据点云数据构建八叉树 pragmaonce include iostream include vector include math h classOctree public Octree Octree destory octree structPoint doublex doubley doublez voidsetPoint doublex doubley math h vector iostream

    2025年10月15日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号