目标:
统计地市的总的完成数量,并根据总数来排序。
要把下面的数据:
[
{
"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 聚合求和,要熟悉第二种方式处理方式,写法比较简便。想不到就用第一种的方式,肯定可以。