利用map 对list进行对某一字段分组

时间:2023-01-21 15:14:10
Map<String, List<NumberAssignVo>> proMap = new HashMap<>();
        for (NumberAssignVo numberAssignVo : numberAssignVos) {
            List<NumberAssignVo> tempList = proMap.get(numberAssignVo.getProviderName());
            if (tempList == null) {
                tempList = new ArrayList<>();
                tempList.add(numberAssignVo);
                proMap.put(numberAssignVo.getProviderName(), tempList);
            } else {
                tempList.add(numberAssignVo);
            }
        }

        Map<String, Map<String, List<NumberAssignVo>>> resultMap = new HashMap<>();
        for(String providerName : proMap.keySet()){
            List<NumberAssignVo> tempList1 = proMap.get(providerName);
            Map<String, List<NumberAssignVo>> countryMap = new HashMap<>();
            for (NumberAssignVo numberAssignVo1 : tempList1) {
                List<NumberAssignVo> tempList2 = countryMap.get(numberAssignVo1.getCountryEnName());
                if (tempList2 == null) {
                    tempList2 = new ArrayList<>();
                    tempList2.add(numberAssignVo1);
                    countryMap.put(numberAssignVo1.getCountryEnName(), tempList2);
                } else {
                    tempList2.add(numberAssignVo1);
                }
            }
            resultMap.put(providerName,countryMap);
        }