java8 list map 聚合求和

目标:

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

要把下面的数据:

[
	{
		"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
	} 
]

目标格式:

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

处理:

模拟数据:

java 复制代码
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());
    }

方式一: 分布处理

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

java 复制代码
  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);

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

方式二:聚合处理

java 复制代码
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"))));

汇总:

java 复制代码
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));
    }

输出:

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

相关推荐
‘’林花谢了春红‘’3 小时前
C++ list (链表)容器
c++·链表·list
阿龟在奔跑6 小时前
引用类型的局部变量线程安全问题分析——以多线程对方法局部变量List类型对象实例的add、remove操作为例
java·jvm·安全·list
ashane13148 小时前
Java list
java·windows·list
djk88889 小时前
.net将List<实体1>的数据转到List<实体2>
数据结构·list·.net
黎明晓月12 小时前
PostgreSQL提取JSON格式的数据(包含提取list指定索引数据)
postgresql·json·list
熬夜学编程的小王15 小时前
【C++篇】从基础到进阶:全面掌握C++ List容器的使用
开发语言·c++·list·双向链表·迭代器失效
@小博的博客1 天前
C++初阶学习第十一弹——list的用法和模拟实现
开发语言·c++·学习·list
努力学习的小廉2 天前
【list的模拟实现】—— 我与C++的模拟实现(十四)
c++·list
会code的厨子3 天前
Java集合分页
java·list
小叶学C++3 天前
【C++】第九节:list
linux·c++·list