Java8 Stream 之groupingBy 分组讲解
本文主要讲解:Java 8 Stream之Collectors.groupingBy()分组示例
Collectors.groupingBy() 分组之常见用法
功能代码:
1 2 3 4 5 6 7 8 9 | /** * 使用java8 stream groupingBy操作,按城市分组list */ public void groupingByCity() { Map<String, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(Employee::getCity)); map.forEach((k, v) -> { System. out .println(k + " = " + v); }); } |
Collectors.groupingBy() 分组之统计每个分组的count
功能代码:
1 2 3 4 5 6 7 8 9 10 11 | /** * 使用java8 stream groupingBy操作,按城市分组list统计count */ public void groupingByCount() { Map<String, Long> map = employees.stream() .collect(Collectors.groupingBy(Employee::getCity, Collectors.counting())); map.forEach((k, v) -> { System. out .println(k + " = " + v); }); } |
Collectors.groupingBy() 分组之统计分组平均值
功能代码:
1 2 3 4 5 6 7 8 9 10 11 | /** * 使用java8 stream groupingBy操作,按城市分组list并计算分组年龄平均值 */ public void groupingByAverage() { Map<String, Double> map = employees.stream() .collect(Collectors.groupingBy(Employee::getCity, Collectors.averagingInt(Employee::getAge))); map.forEach((k, v) -> { System. out .println(k + " = " + v); }); } |
Collectors.groupingBy() 分组之统计分组总值
功能代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * 使用java8 stream groupingBy操作,按城市分组list并计算分组销售总值 */ public void groupingBySum() { Map<String, Long> map = employees.stream() .collect(Collectors.groupingBy(Employee::getCity, Collectors.summingLong(Employee::getAmount))); map.forEach((k, v) -> { System. out .println(k + " = " + v); }); // 对Map按照分组销售总值逆序排序 Map<String, Long> sortedMap = new LinkedHashMap<>(); map.entrySet().stream().sorted(Map.Entry.<String, Long> comparingByValue().reversed()) .forEachOrdered(e -> sortedMap.put(e.getKey(), e.getValue())); sortedMap.forEach((k, v) -> { System. out .println(k + " = " + v); }); } |
Collectors.groupingBy() 分组之Join分组List
功能代码:
1 2 3 4 5 6 7 8 9 10 11 | /** * 使用java8 stream groupingBy操作,按城市分组list并通过join操作连接分组list中的对象的name 属性使用逗号分隔 */ public void groupingByString() { Map<String, String> map = employees.stream().collect(Collectors.groupingBy(Employee::getCity, Collectors.mapping(Employee::getName, Collectors.joining( ", " , "Names: [" , "]" )))); map.forEach((k, v) -> { System. out .println(k + " = " + v); }); } |
Collectors.groupingBy() 分组之转换分组结果List -> List
功能代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * 使用java8 stream groupingBy操作,按城市分组list,将List转化为name的List */ public void groupingByList() { Map<String, List<String>> map = employees.stream().collect( Collectors.groupingBy(Employee::getCity, Collectors.mapping(Employee::getName, Collectors.toList()))); map.forEach((k, v) -> { System. out .println(k + " = " + v); v.stream().forEach(item -> { System. out .println( "item = " + item); }); }); } |
Collectors.groupingBy() 分组之转换分组结果List -> Set
功能代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * 使用java8 stream groupingBy操作,按城市分组list,将List转化为name的Set */ public void groupingBySet() { Map<String, Set<String>> map = employees.stream().collect( Collectors.groupingBy(Employee::getCity, Collectors.mapping(Employee::getName, Collectors.toSet()))); map.forEach((k, v) -> { System. out .println(k + " = " + v); v.stream().forEach(item -> { System. out .println( "item = " + item); }); }); } |
Collectors.groupingBy() 分组之使用对象分组List
功能代码:
1 2 3 4 5 6 7 8 9 10 11 12 | /** * 使用java8 stream groupingBy操作,通过Object对象的成员分组List */ public void groupingByObject() { Map<Manage, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(item -> { return new Manage(item.getName()); })); map.forEach((k, v) -> { System. out .println(k + " = " + v); }); } |
Collectors.groupingBy() 分组之使用两个成员分组List
功能代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * 使用java8 stream groupingBy操作, 基于city 和name 实现多次分组 */ public void groupingBys() { Map<String, Map<String, List<Employee>>> map = employees.stream() .collect(Collectors.groupingBy(Employee::getCity, Collectors.groupingBy(Employee::getName))); map.forEach((k, v) -> { System. out .println(k + " = " + v); v.forEach((i, j) -> { System. out .println(i + " = " + j); }); }); } |
自定义Distinct对结果去重
功能代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * 使用java8 stream groupingBy操作, 基于Distinct 去重数据 */ public void groupingByDistinct() { List<Employee> list = employees.stream().filter(distinctByKey(Employee :: getCity)) .collect(Collectors.toList());; list.stream().forEach(item->{ System. out .println( "city = " + item.getCity()); }); } /** * 自定义重复key 规则 * @param keyExtractor * @return */ private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { Set<Object> seen = ConcurrentHashMap.newKeySet(); return t -> seen.add(keyExtractor.apply(t)); } |
原文地址:https://blog.csdn.net/zhouzhiwengang/article/details/112319054
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)