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

相关推荐
南城花随雪。5 小时前
哈希表(Hash Table)、跳表(Skip List) 和 有序字典(Ordered Dictionary) 的详细介绍
list·哈希算法·散列表
Trouvaille ~18 小时前
【C++篇】在秩序与混沌的交响乐中: STL之map容器的哲学探寻
开发语言·数据结构·c++·算法·迭代器模式·stl·map
算法与编程之美1 天前
list与iterator的之间的区别,如何用斐波那契数列探索yield
数据结构·list
初次见面我叫泰隆2 天前
C++STL——list
开发语言·c++·list
杀神lwz2 天前
Java List根据属性排序
java·python·list
fs哆哆3 天前
C#编程:VSTO在Excel工作表中输出List数据
服务器·windows·c#·list·excel
小李飞刀李寻欢4 天前
vm.max_map_count 表示啥意思啊?通俗易懂点,有单位么?262144表示啥意思?
jvm·elasticsearch·内存·map·es·服务
Liknana4 天前
List 列表基础用法
windows·python·list
初次见面我叫泰隆4 天前
C++模拟实现list
c++·list