Map实现分组

85次阅读

共计 3515 个字符,预计需要花费 9 分钟才能阅读完成。

利用 Map 对数据进行分组,对数字类型字段求平均值,或者求和。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class MapGroupViaLam {public static void main(String[] args) {List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        putValInto(list);
        List<String> groupKeys = new ArrayList<>(Arrays.asList(new String[] {"PROVICE", "CITY"}));
        String groupNameStr = reBuildGroupMap(list, groupKeys);
        
        System.out.println("-------- 分组前 -------------");
        list.forEach(x -> {System.out.println(x);
        });
        
        List<Map<String, Object>> result = new ArrayList<>();
        Map<String, List<Map<String, Object>>> glist = list.stream()
                .collect(Collectors.groupingBy(e -> e.get(groupNameStr).toString()));

        glist.forEach((k, slist) -> {Map<String, Object> nmap = new HashMap<>();
            DoubleSummaryStatistics sumcc = slist.stream()
                    .collect(Collectors.summarizingDouble(e -> Double.valueOf(e.get("NUM1").toString())));
            DoubleSummaryStatistics sumcc2 = slist.stream()
                    .collect(Collectors.summarizingDouble(e -> Double.valueOf(e.get("NUM2").toString())));
            nmap.put("PROVICE", slist.get(0).get("PROVICE"));
            nmap.put("CITY", slist.get(0).get("CITY"));
            nmap.put("NUM1", sumcc.getSum());// 求和
            nmap.put("NUM2", sumcc2.getAverage());// 求和
            result.add(nmap);

        });
        System.out.println("-------- 分组后 -------------");
        result.forEach(x -> {System.out.println(x);
        });

    }

    /**
     * 重建需要 group by 的 map 添加一列数据保存所有 groupby 拼接字段
     * 
     * @param excelDatas
     * @param groupKeys
     */
    private static String reBuildGroupMap(List<Map<String, Object>> excelDatas, List<String> groupKeys) {
        String groupStr = "";
        if (excelDatas != null && !excelDatas.isEmpty() && groupKeys != null && !groupKeys.isEmpty()) {StringBuffer newKey = new StringBuffer();
            StringBuffer newVal = new StringBuffer();
            for (int i = 0, excelDataSize = excelDatas.size(); i < excelDataSize; i++) {Map<String, Object> excelData = excelDatas.get(i);
                Set<Map.Entry<String, Object>> eSets = excelData.entrySet();
                for (Map.Entry<String, Object> entry : eSets) {String key = entry.getKey();
                    String val = entry.getValue() == null ? "" : entry.getValue().toString();
                    if (groupKeys.contains(key)) {newKey.append(key);
                        newVal.append(val);
                    }
                }
                if (newKey.length() > 0 && newVal.length() > 0) {if (i==0) {groupStr = newKey.toString();    
                    }
                    excelData.put(newKey.toString(), newVal.toString());
                }
                newKey.setLength(0);
                newVal.setLength(0);
            }
        }
        return groupStr;
    }

    private static void putValInto(List<Map<String, Object>> excelDatas) {Map<String, Object> excelMap1 = new HashMap<String, Object>();
        excelMap1.put("PROVICE", "四川省");
        excelMap1.put("CITY", "成都市");
        excelMap1.put("NUM1", "100");
        excelMap1.put("NUM2", "100");
        excelDatas.add(excelMap1);

        Map<String, Object> excelMap2 = new HashMap<String, Object>();
        excelMap2.put("PROVICE", "四川省");
        excelMap2.put("CITY", "南充市");
        excelMap2.put("NUM1", "200");
        excelMap2.put("NUM2", "200");
        excelDatas.add(excelMap2);

        Map<String, Object> excelMap3 = new HashMap<String, Object>();
        excelMap3.put("PROVICE", "四川省");
        excelMap3.put("CITY", "南充市");
        excelMap3.put("NUM1", "300");
        excelMap3.put("NUM2", "300");
        excelDatas.add(excelMap3);

        Map<String, Object> excelMap4 = new HashMap<String, Object>();
        excelMap4.put("PROVICE", "河南省");
        excelMap4.put("CITY", "郑州市");
        excelMap4.put("NUM1", "400");
        excelMap4.put("NUM2", "400");
        excelDatas.add(excelMap4);

        Map<String, Object> excelMap5 = new HashMap<String, Object>();
        excelMap5.put("PROVICE", "河南省");
        excelMap5.put("CITY", "郑州市");
        excelMap5.put("NUM1", "500");
        excelMap5.put("NUM2", "500");
        excelDatas.add(excelMap5);

        Map<String, Object> excelMap6 = new HashMap<String, Object>();
        excelMap6.put("PROVICE", "河南省");
        excelMap6.put("CITY", "南阳市");
        excelMap6.put("NUM1", "600");
        excelMap6.put("NUM2", "600");
        excelDatas.add(excelMap6);
    }

}

正文完
 0