【笔记】JAVA 1.8 Collectors.groupingBy
Map<String, List<Product>> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory));Map<String, List<Product>> prodMap = prodList.stream().collect(Collectors.groupingBy(item -> item.getCategory() + "_" + item.getName()));
Map<String, List<DdDistrict>> dMap = find(ddDistrict).stream().collect(
Collectors.groupingBy(DdDistrict::getParentId, LinkedHashMap::new, Collectors.toList()));// 按照条件分组
Map<String, Map<String, List<Product>>> prodMap= prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.groupingBy(item -> {
if(item.getNum() < 3) {
return "3";
}else {
return "other";
}
})));
//{"啤酒":{"other":[{"category":"啤酒","id":4,"name":"青岛啤酒","num":3,"price":10},{"category":"啤酒","id":5,"name":"百威啤酒","num":10,"price":15}]},"零食":{"other":[{"category":"零食","id":3,"name":"月饼","num":3,"price":30}],"3":[{"category":"零食","id":1,"name":"面包","num":1,"price":15.5},{"category":"零食","id":2,"name":"饼干","num":2,"price":20}]}}
// 要实现多级分组,我们可以使用一个由双参数版本的Collectors.groupingBy工厂方法创 建的收集器,它除了普通的分类函数之外,还可以接受collector类型的第二个参数。那么要进 行二级分组的话,我们可以把一个内层groupingBy传递给外层groupingBy,并定义一个为流 中项目分类的二级标准。
Map<String, List<Product>> prodMap= prodList.stream().collect(Collectors.groupingBy(item -> {
if(item.getNum() < 3) {
return "3";
}else {
return "other";
}
}));
//{"other":[{"category":"零食","id":3,"name":"月饼","num":3,"price":30},{"category":"啤酒","id":4,"name":"青岛啤酒","num":3,"price":10},{"category":"啤酒","id":5,"name":"百威啤酒","num":10,"price":15}],"3":[{"category":"零食","id":1,"name":"面包","num":1,"price":15.5},{"category":"零食","id":2,"name":"饼干","num":2,"price":20}]}
// 按子组收集数据
// 求总数
Map<String, Long> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.counting()));
// 把收集器的结果转换为另一种类型
Map<String, Product> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparingInt(Product::getNum)), Optional::get)));
//{"啤酒":{"category":"啤酒","id":5,"name":"百威啤酒","num":10,"price":15},"零食":{"category":"零食","id":3,"name":"月饼","num":3,"price":30}}
// 联合其他收集器
Map<String, Set<String>> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.mapping(Product::getName, Collectors.toSet())));
//{"啤酒":["青岛啤酒","百威啤酒"],"零食":["面包","饼干","月饼"]}
// 多层分组
/* Map>>按用户分组,按类型分组,组装:每个用户、每个类型下的属性值列表*/
Map>> userAttrMap = userAttrList.stream().collect(
Collectors.groupingBy(IapUserIndustryAttrRel :: getUserId,
Collectors.groupingBy(IapUserIndustryAttrRel :: getIndustryTypeId,
Collectors.mapping( IapUserIndustryAttrRel :: getIndustryAttributeId, Collectors.toList() )
)
)
);
// 多层分组示例
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=, 2=}}
// 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));
// 输出结果:{"001":{"1":{"Y1":"1","Y2":"5"},"2":{"Y1":"2"}},"002":{"1":{"Y1":"3","Y2":"6"},"2":{"Y1":"4"}}}
//{"啤酒":2,"零食":3}
// 求和
Map<String, Integer> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.summingInt(Product::getNum)));
//{"啤酒":13,"零食":6}
页:
[1]