您的位置:首页 > 游戏 > 游戏 > 建筑效果图_深圳市建网站公司_万能浏览器_搜狗网站收录

建筑效果图_深圳市建网站公司_万能浏览器_搜狗网站收录

2024/11/18 8:48:13 来源:https://blog.csdn.net/qq_35461948/article/details/139353281  浏览:    关键词:建筑效果图_深圳市建网站公司_万能浏览器_搜狗网站收录
建筑效果图_深圳市建网站公司_万能浏览器_搜狗网站收录

目标:

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

要把下面的数据:

[{"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 聚合求和,要熟悉第二种方式处理方式,写法比较简便。想不到就用第一种的方式,肯定可以。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com