public static void main(String[] args) {List<IapUserIndustryAttrRel> userAttrList = new ArrayList<>();IapUserIndustryAttrRel userAttr1 = new IapUserIndustryAttrRel();userAttr1.setUserId("100001");userAttr1.setIndustryTypeId("1");userAttr1.setIndustryAttributeId("1");userAttrList.add(userAttr1);IapUserIndustryAttrRel userAttr2 = new IapUserIndustryAttrRel();userAttr2.setUserId("100001");userAttr2.setIndustryTypeId("1");userAttr2.setIndustryAttributeId("2");userAttrList.add(userAttr2);IapUserIndustryAttrRel userAttr3 = new IapUserIndustryAttrRel();userAttr3.setUserId("100001");userAttr3.setIndustryTypeId("2");userAttr3.setIndustryAttributeId("3");userAttrList.add(userAttr3);Map<String, Map<String, List<String>>> userAttrMap = userAttrList.stream().collect(Collectors.groupingBy(IapUserIndustryAttrRel :: getUserId,Collectors.groupingBy(IapUserIndustryAttrRel :: getIndustryTypeId,Collectors.mapping(IapUserIndustryAttrRel :: getIndustryAttributeId, Collectors.toList()))));System.out.println(userAttrMap);}
輸出結果:
{100001={1=[1, 2], 2=[3]}}
Map>>?按機構號分組,按渠道號分組,組裝:每個機構、每個渠道下的? 產品信息(map)。
Test t1= new Test("001","1","Y1","1");Test t2= new Test("001","2","Y1","2");Test t3= new Test("002","1","Y1","3");Test t4= new Test("002","2","Y1","4");Test t5= new Test("001","1","Y2","5");Test t6= new Test("002","1","Y2","6");List<Test> list = new ArrayList<>();list.add(t1);list.add(t2);list.add(t3);list.add(t4);list.add(t5);list.add(t6);Map<String, Map<String, Map<String, String>>> collect = list.stream().collect(Collectors.groupingBy(Test::getOrgCode, Collectors.groupingBy(Test::getChannelId, Collectors.toMap(Test::getProductCode, Test::getD))));System.out.println(JSON.toJSON(collect));