java8中的Collectors.groupingBy用法詳解

Collectors.groupingBy根據一個或多個屬性對集合中的項目進行分組

數據準備:

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);

分組

按照類目分組:

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}]}

多級分組

要實現多級分組,我們可以使用一個由雙參數版本的Collectors.groupingBy工廠方法創 建的收集器,它除瞭普通的分類函數之外,還可以接受collector類型的第二個參數。那麼要進 行二級分組的話,我們可以把一個內層groupingBy傳遞給外層groupingBy,並定義一個為流 中項目分類的二級標準。

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}]}}

按子組收集數據

求總數

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}

把收集器的結果轉換為另一種類型

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())));
 
//{"啤酒":["青島啤酒","百威啤酒"],"零食":["面包","餅幹","月餅"]}

到此這篇關於java8中的Collectors.groupingBy用法詳解的文章就介紹到這瞭,更多相關java8 Collectors.groupingBy 內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: