java8 list map 聚合求和

ops/2024/11/2 13:10:42/

目标:

统计地市的总的完成数量,并根据总数来排序。

要把下面的数据:

[{"city_name": "南京市","carrier_id": "2","carrier_name": "移动","city_id": "0","finish_num": 14},{"city_name": "南京市","carrier_id": "5","carrier_name": "移动转售企业","city_id": "0","finish_num": 1},{"city_name": "南京市","carrier_id": "6","carrier_name": "长城宽带","city_id": "0","finish_num": 1},{"city_name": "南京市","carrier_id": "7","carrier_name": "增值电信企业","city_id": "0","finish_num": 1},{"city_name": "无锡市","carrier_id": "1","carrier_name": "电信","city_id": "1","finish_num": 3},{"city_name": "镇江市","carrier_id": "1","carrier_name": "电信","city_id": "10","finish_num": 1}, {"city_name": "常州市","carrier_id": "2","carrier_name": "移动","city_id": "3","finish_num": 1},{"city_name": "常州市","carrier_id": "1","carrier_name": "电信","city_id": "3","finish_num": 1},{"city_name": "常州市","carrier_id": "5","carrier_name": "移动转售企业","city_id": "3","finish_num": 1} 
]

目标格式:
 

[{"city_name": "南京市","total": 17,"city_id": "0"},{"city_name": "常州市","total": 5,"city_id": "3"},{"city_name": "无锡市","total": 3,"city_id": "1"}
]

处理:

模拟数据:

public static List<Map<String, Object>> initData() {String data = "[\n" +"\t{\n" +"\t\t\"city_name\": \"南京市\",\n" +"\t\t\"carrier_id\": \"2\",\n" +"\t\t\"carrier_name\": \"移动\",\n" +"\t\t\"city_id\": \"0\",\n" +"\t\t\"finish_num\": 14\n" +"\t},\n" +"\t{\n" +"\t\t\"city_name\": \"南京市\",\n" +"\t\t\"carrier_id\": \"5\",\n" +"\t\t\"carrier_name\": \"移动转售企业\",\n" +"\t\t\"city_id\": \"0\",\n" +"\t\t\"finish_num\": 1\n" +"\t},\n" +"\t{\n" +"\t\t\"city_name\": \"南京市\",\n" +"\t\t\"carrier_id\": \"6\",\n" +"\t\t\"carrier_name\": \"长城宽带\",\n" +"\t\t\"city_id\": \"0\",\n" +"\t\t\"finish_num\": 1\n" +"\t},\n" +"\t{\n" +"\t\t\"city_name\": \"南京市\",\n" +"\t\t\"carrier_id\": \"7\",\n" +"\t\t\"carrier_name\": \"增值电信企业\",\n" +"\t\t\"city_id\": \"0\",\n" +"\t\t\"finish_num\": 1\n" +"\t},\n" +"\t{\n" +"\t\t\"city_name\": \"无锡市\",\n" +"\t\t\"carrier_id\": \"1\",\n" +"\t\t\"carrier_name\": \"电信\",\n" +"\t\t\"city_id\": \"1\",\n" +"\t\t\"finish_num\": 3\n" +"\t},\n" +"\t{\n" +"\t\t\"city_name\": \"镇江市\",\n" +"\t\t\"carrier_id\": \"1\",\n" +"\t\t\"carrier_name\": \"电信\",\n" +"\t\t\"city_id\": \"10\",\n" +"\t\t\"finish_num\": 1\n" +"\t},\n" +"\t{\n" +"\t\t\"city_name\": \"宿迁市\",\n" +"\t\t\"carrier_id\": \"3\",\n" +"\t\t\"carrier_name\": \"联通\",\n" +"\t\t\"city_id\": \"12\",\n" +"\t\t\"finish_num\": 1\n" +"\t},\n" +"\t{\n" +"\t\t\"city_name\": \"外省\",\n" +"\t\t\"carrier_id\": \"1\",\n" +"\t\t\"carrier_name\": \"电信\",\n" +"\t\t\"city_id\": \"13\",\n" +"\t\t\"finish_num\": 3\n" +"\t},\n" +"\t{\n" +"\t\t\"city_name\": \"徐州市\",\n" +"\t\t\"carrier_id\": \"3\",\n" +"\t\t\"carrier_name\": \"联通\",\n" +"\t\t\"city_id\": \"2\",\n" +"\t\t\"finish_num\": 2\n" +"\t},\n" +"\t{\n" +"\t\t\"city_name\": \"常州市\",\n" +"\t\t\"carrier_id\": \"2\",\n" +"\t\t\"carrier_name\": \"移动\",\n" +"\t\t\"city_id\": \"3\",\n" +"\t\t\"finish_num\": 1\n" +"\t},\n" +"\t{\n" +"\t\t\"city_name\": \"常州市\",\n" +"\t\t\"carrier_id\": \"1\",\n" +"\t\t\"carrier_name\": \"电信\",\n" +"\t\t\"city_id\": \"3\",\n" +"\t\t\"finish_num\": 1\n" +"\t},\n" +"\t{\n" +"\t\t\"city_name\": \"常州市\",\n" +"\t\t\"carrier_id\": \"5\",\n" +"\t\t\"carrier_name\": \"移动转售企业\",\n" +"\t\t\"city_id\": \"3\",\n" +"\t\t\"finish_num\": 1\n" +"\t},\n" +"\t{\n" +"\t\t\"city_name\": \"常州市\",\n" +"\t\t\"carrier_id\": \"6\",\n" +"\t\t\"carrier_name\": \"长城宽带\",\n" +"\t\t\"city_id\": \"3\",\n" +"\t\t\"finish_num\": 1\n" +"\t},\n" +"\t{\n" +"\t\t\"city_name\": \"常州市\",\n" +"\t\t\"carrier_id\": \"7\",\n" +"\t\t\"carrier_name\": \"增值电信企业\",\n" +"\t\t\"city_id\": \"3\",\n" +"\t\t\"finish_num\": 1\n" +"\t}\n" +"]";return changeFormat(data);}private static List<Map<String,Object>> changeFormat(String areaInfo){JSONArray areaArr = JSONArray.parseArray(areaInfo);return ListUtils.emptyIfNull(areaArr).stream().map(e -> (JSONObject) e).map(e -> (Map<String, Object>)JSONObject.parseObject( e.toJSONString())).collect(Collectors.toList());}

方式一: 分布处理

先根据市进行聚合,再根据数量进行求和

  Map<String, List<Map<String, Object>>> cityGroup = ListUtils.emptyIfNull(cityCarrier).stream().collect(Collectors.groupingBy(e -> e.get("city_id").toString()));Map<String, Integer> citySumMap = MapUtils.emptyIfNull(cityGroup).entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, t -> 
ListUtils.emptyIfNull(t.getValue()).stream().mapToInt(f -> 
MapUtils.getInteger(f, "finish_num")).sum()));System.out.println(citySumMap);

 能分步处理,说明可以聚合到一起处理

方式二:聚合处理

Map<String, IntSummaryStatistics> citySumMap2 = 
ListUtils.emptyIfNull(cityCarrier).stream()          
.collect(Collectors.groupingBy(e -> e.get("city_id").toString(),                  
Collectors.summarizingInt(f -> MapUtils.getInteger(f, "finish_num"))));

汇总:

public static void main(String[] args) {List<Map<String, Object>> cityCarrier = initData();Map<String, String> cityNameMap = ListUtils.emptyIfNull(cityCarrier).stream().collect(Collectors.toMap(e -> MapUtils.getString(e, "city_id"), f -> MapUtils.getString(f, "city_name"), (x, y) -> x));Map<String, IntSummaryStatistics> citySumMap2 = ListUtils.emptyIfNull(cityCarrier).stream().collect(Collectors.groupingBy(e -> e.get("city_id").toString(),Collectors.summarizingInt(f -> MapUtils.getInteger(f, "finish_num"))));List<Map<String, Object>> cityCountData = MapUtils.emptyIfNull(cityNameMap).entrySet().stream().map(e -> {String key = e.getKey();Map<String, Object> temp = new HashMap<>();temp.put("city_id", key);temp.put("city_name", e.getValue());temp.put("total", citySumMap2.get(key).getSum());return temp;}).collect(Collectors.toList());List<Map<String, Object>> result = ListUtils.emptyIfNull(cityCountData).stream().sorted((c1, c2) -> MapUtils.getDouble(c2, "total").compareTo(MapUtils.getDouble(c1, "total"))).collect(Collectors.toList());System.out.println(JSON.toJSONString(result));}

输出:

[{"city_name": "南京市","total": 17,"city_id": "0"},{"city_name": "常州市","total": 5,"city_id": "3"},{"city_name": "无锡市","total": 3,"city_id": "1"},{"city_name": "外省","total": 3,"city_id": "13"},{"city_name": "徐州市","total": 2,"city_id": "2"},{"city_name": "宿迁市","total": 1,"city_id": "12"},{"city_name": "镇江市","total": 1,"city_id": "10"}
]

这样就达到目的了。

总结:

        list map 聚合求和,要熟悉第二种方式处理方式,写法比较简便。想不到就用第一种的方式,肯定可以。


http://www.ppmy.cn/ops/130435.html

相关文章

【Python游戏开发】猜数字游戏

【Python游戏开发】猜数字游戏 前言正文核心函数思考步骤实现原理代码实现结论前言 在这个快节奏的世界里,我们都需要一些能放松大脑的小游戏。你有没有想过,能不能通过一些简单的猜谜来激发自己的思维?没错,就是我们熟悉的"猜数字"游戏!这个游戏看似简单,但实…

【MySql】第五章(存储函数与存储过程)

文章目录 一、常用的系统函数1.1 数学函数1.2 字符串函数1.2.1 计算字符串字符数的函数和计算字符串长度的函数1.2.2 字符串合并函数1.2.3 字符串大小写转换函数1.2.4 删除空格函数1.2.5 取子串函数1.2.6 其他字符串函数 1.3 日期和时间函数1.3.1 获取当前系统日期及指定日期年…

「Mac畅玩鸿蒙与硬件18」鸿蒙UI组件篇8 - 高级动画效果与缓动控制

高级动画可以显著提升用户体验,为应用界面带来更流畅的视觉效果。本篇将深入介绍鸿蒙框架的高级动画,包括弹性动画、透明度渐变和旋转缩放组合动画等示例。 关键词 高级动画弹性缓动自动动画缓动曲线一、Animation 组件的高级缓动曲线 缓动曲线(Easing Curve)控制动画在不…

Hive的数据存储格式

目录 一、前言 二、存储格式 2.1、文本格式&#xff08;TextFile&#xff09; 2.1.1、定义与特点 2.1.2、存储与压缩 2. 1.3、使用场景 2.2、行列式文件&#xff08;ORCFile&#xff09; 2.2.1、ORC的结构 2.2.2、ORC的数据类型 2.2.3、ORC的压缩格式 2.2.3、ORC存储…

自然语言处理领域中的两个主要技术挑战:实体歧义和上下文管理

自然语言处理领域中的两个主要技术挑战&#xff1a;实体歧义和上下文管理 这段话详尽地讨论了在自然语言处理领域中的两个主要技术挑战&#xff1a;实体歧义和上下文管理。具体地&#xff0c;它解释了如何识别并解决在同一句子中相同日期和地点被赋予多种不同含义的问题。此处…

DNS域名解析服务器

一.DNS简介 DNS&#xff08;Domain Name System&#xff09;是互联网上的一项服务&#xff0c;它作为将域名和IP地址相互映射的一个分布式 数据库&#xff0c;能够使人更方便的访问互联网。 DNS系统使用的是网络的查询&#xff0c;那么自然需要有监听的port。DNS使用的是53端口…

Lucene的概述与应用场景(1)

文章目录 第1章 Lucene概述1.1 搜索的实现方案1.1.1 传统实现方案1.1.2 Lucene实现方案 1.2 数据查询方法1.1.1 顺序扫描法1.1.2 倒排索引法 1.3 Lucene相关概念1.3.1 文档对象1.3.2 域对象1&#xff09;分词2&#xff09;索引3&#xff09;存储 1.3.3 常用的Field种类 1.4 分词…

Halcon-模板匹配(WPF)

halcon的代码 dev_open_window (0, 0, 512, 512, black, WindowHandle) read_image (Image, C:/Users/CF/Desktop/image.jpg) dev_display (Image)draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2) gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2) r…