怎么使用Java Stream API将List按自定义分组规则转换成Map
发表于:2025-11-14 作者:千家信息网编辑
千家信息网最后更新 2025年11月14日,小编给大家分享一下怎么使用Java Stream API将List按自定义分组规则转换成Map,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们
千家信息网最后更新 2025年11月14日怎么使用Java Stream API将List按自定义分组规则转换成Map
小编给大家分享一下怎么使用Java Stream API将List按自定义分组规则转换成Map,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
测试数据是List里的4个员工对象实例:

根据员工所在的城市进行分组:
结果分成了三组:
第一组的员工在上海:

第二组的员工在成都:
统计每组员工个数:
把员工进行分组,得分大于101分的在一组,小于等于101的在另一组:
分组结果:
package java8;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.function.Consumer;import java.util.stream.Collectors;class Employee { private String city; private String name; private int score; public Employee(String name, String city, int score){ this.city = city; this.name = name; this.score = score; } public String getCity(){ System.out.println("city: " + this.city); return this.city; } public String getName() { return this.name; } public int getScore() { return this.score; } @Override public String toString() { return String.format("Employee: " + this.name + " city: " + this.city); }}class Person { private String name; private int age; Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return String.format("Person{name='%s', age=%d}", name, age); }}// Jerry 2016-01-15 20:51PM ? 多用于extends generic的type,接受所有Object的sub classpublic class StreamTest { private static void printMap(Map extends Object, ? extends Object> map) { for(Entry extends Object, ? extends Object> entry:map.entrySet()) { System.out.println("key = " + entry.getKey() + " , Value = " + entry.getValue()); } } public static void main(String[] args) { ArrayList employees = new ArrayList(); employees.add(new Employee("A", "Shanghai",100)); employees.add(new Employee("B", "Chengdu",101)); employees.add(new Employee("C", "Shenzhen",102)); employees.add(new Employee("D", "Chengdu",104)); // group by City Map> employeesByCity = employees.stream().collect( Collectors.groupingBy(Employee::getCity)); // default void forEach(Consumer super T> action) { for(Map.Entry> entry:employeesByCity.entrySet()) { System.out.println("key= " + entry.getKey() + " , Value = " + entry.getValue()); entry.getValue().forEach(System.out::println); } // 2016-01-15 20:37PM Consumer aa = a -> { System.out.println("Employee: " + a.getName() + " : " + a.getScore()); }; List chengduEmployee = employeesByCity.get("Chengdu"); chengduEmployee.forEach(aa); // test for counting Map employeesByCity2 = employees.stream().collect( Collectors.groupingBy(Employee::getCity, Collectors.counting())); printMap(employeesByCity2); // calculate average score Map employeesByCity3 = employees.stream().collect( Collectors.groupingBy(Employee::getCity, Collectors.averagingInt(Employee::getScore))); printMap(employeesByCity3); /*Stream people = Stream.of(new Person("Paul", 24), new Person("Mark", 30), new Person("Will", 28)); Map> peopleByAge = people.collect(groupingBy(p -> p.age, mapping((Person p) -> p.name, toList()))); System.out.println(peopleByAge);*/ /* * 分区是一种特殊的分组,结果 map 至少包含两个不同的分组--一个true,一个false。 * 例如,如果想找出最优秀的员工,你可以将所有雇员分为两组,一组销售量大于 N, * 另一组小于 N,使用 partitioningBy 收集器: */ System.out.println("partition result"); Map> partitioned = employees.stream().collect(Collectors.partitioningBy(e -> e.getScore() > 101)); printMap(partitioned); /* * 你也可以将 groupingBy 收集器传递给 partitioningBy 收集器来将联合使用分区和分组。例如,你可以统计每个分区中的每个城市的雇员人数: Map> result = employees.stream().collect(partitioningBy(e -> e.getNumSales() > 150, groupingBy(Employee::getCity, counting()))); 这样会生成一个二级 Map: {false={London=1}, true={New York=1, Hong Kong=1, London=1}} */ }} 以上是"怎么使用Java Stream API将List按自定义分组规则转换成Map"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
分组
员工
篇文章
结果
收集器
规则
内容
城市
雇员
统计
不同
优秀
特殊
不怎么
两个
个数
人数
大部分
实例
对象
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
医疗软件开发是做什么的
软件开发 科目
跨数据库触发器
临泉软件开发商
海口有哪些软件开发公司介绍
数据库的两种建立方式
数据库中有哪几个对象
服务器保护法律
手机显示服务器出错怎么设置
保妇康栓药品广告数据库
沧州真格网络技术6
生鲜柜共享软件开发
名利软件开发
数据库pc技术
网络技术基础教程 pdf
资源环境遥感数据库
镇江智能软件开发用途
服务器抢手机
安徽社区党性体检软件开发
闵行区推广软件开发售后保障
无线网络安全监管定制
怎么从打卡机里导出数据库
广东纤皙美互联网科技
国家网络安全法的知识题解答
数据库查询并让数据倒序输出
软件开发分享
网络安全专责职责
hp服务器监控
双流网络安全征集
服务器怎么做挂机宝