目錄
1、數(shù)據(jù)準(zhǔn)備:
2、分組
按照類目分組:
按照幾個屬性拼接分組:
根據(jù)不同條件分組
3、多級分組
4、按子組收集數(shù)據(jù)
求總數(shù)
求和
把收集器的結(jié)果轉(zhuǎn)換為另一種類型
聯(lián)合其他收集器
Collectors.groupingBy根據(jù)一個或多個屬性對集合中的項目進行分組
1、數(shù)據(jù)準(zhǔn)備:
public Product(Long id, Integer num, BigDecimal price, String name, String category) {this.id = id;this.num = num;this.price = price;this.name = name;this.category = category;
}Product prod1 = new Product(1L, 1, new BigDecimal("15.5"), "面包", "零食");
Product prod2 = new Product(2L, 2, new BigDecimal("20"), "餅干", "零食");
Product prod3 = new Product(3L, 3, new BigDecimal("30"), "月餅", "零食");
Product prod4 = new Product(4L, 3, new BigDecimal("10"), "青島啤酒", "啤酒");
Product prod5 = new Product(5L, 10, new BigDecimal("15"), "百威啤酒", "啤酒");
List<Product> prodList = Lists.newArrayList(prod1, prod2, prod3, prod4, prod5);
2、分組
Map<String, List<Product>> prodMap= prodList.stream().collect(Collectors.groupingBy(Product::getCategory));//{"啤酒":[{"category":"啤酒","id":4,"name":"青島啤酒","num":3,"price":10},{"category":"啤酒","id":5,"name":"百威啤酒","num":10,"price":15}],"零食":[{"category":"零食","id":1,"name":"面包","num":1,"price":15.5},{"category":"零食","id":2,"name":"餅干","num":2,"price":20},{"category":"零食","id":3,"name":"月餅","num":3,"price":30}]}
Map<String, List<Product>> prodMap = prodList.stream().collect(Collectors.groupingBy(item -> item.getCategory() + "_" + item.getName()));//{"零食_月餅":[{"category":"零食","id":3,"name":"月餅","num":3,"price":30}],"零食_面包":[{"category":"零食","id":1,"name":"面包","num":1,"price":15.5}],"啤酒_百威啤酒":[{"category":"啤酒","id":5,"name":"百威啤酒","num":10,"price":15}],"啤酒_青島啤酒":[{"category":"啤酒","id":4,"name":"青島啤酒","num":3,"price":10}],"零食_餅干":[{"category":"零食","id":2,"name":"餅干","num":2,"price":20}]}
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}]}
3、多級分組
要實現(xiàn)多級分組,我們可以使用一個由雙參數(shù)版本的Collectors.groupingBy工廠方法創(chuàng) 建的收集器,它除了普通的分類函數(shù)之外,還可以接受collector類型的第二個參數(shù)。那么要進 行二級分組的話,我們可以把一個內(nèi)層groupingBy傳遞給外層groupingBy,并定義一個為流 中項目分類的二級標(biāo)準(zhǔn)。
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}]}}
4、按子組收集數(shù)據(jù)
Map<String, Long> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.counting()));//{"啤酒":2,"零食":3}
Map<String, Integer> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.summingInt(Product::getNum)));//{"啤酒":13,"零食":6}
把收集器的結(jié)果轉(zhuǎn)換為另一種類型
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())));//{"啤酒":["青島啤酒","百威啤酒"],"零食":["面包","餅干","月餅"]}
5、多層分組
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);}
輸出結(jié)果:
{100001={1=[1, 2], 2=[3]}}
Map>>? 按機構(gòu)號分組,按渠道號分組,組裝:每個機構(gòu)、每個渠道下的? 產(chǎn)品信息(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));
輸出結(jié)果:
{"001":{"1":{"Y1":"1","Y2":"5"},"2":{"Y1":"2"}},"002":{"1":{"Y1":"3","Y2":"6"},"2":{"Y1":"4"}}}
相關(guān)鏈接: java8中map新增方法詳解 java8中Stream的使用 java8中Collection新增方法詳解 java8中Collectors的方法使用實例 java8中常用函數(shù)式接口 java8中的方法引用和構(gòu)造函數(shù)引用 java8中的Collectors.groupingBy用法 java8中的Optional用法 java8中的日期和時間API ?
總結(jié)
以上是生活随笔 為你收集整理的java8中 Collectors.groupingBy用法 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。