java8中的Collectors.groupingBy怎么用
这篇“java8中的Collectors.groupingBy怎么用”文章,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要参考一下,对于“java8中的Collectors.groupingBy怎么用”,小编整理了以下知识点,请大家跟着小编的步伐一步一步的慢慢理解,接下来就让我们进入主题吧。
Collectors.groupingBy根据一个或多个属性对集合中的项目进行分组
数据准备:
publicProduct(Longid,Integernum,BigDecimalprice,Stringname,Stringcategory){ this.id=id; this.num=num; this.price=price; this.name=name; this.category=category; } Productprod1=newProduct(1L,1,newBigDecimal("15.5"),"面包","零食"); Productprod2=newProduct(2L,2,newBigDecimal("20"),"饼干","零食"); Productprod3=newProduct(3L,3,newBigDecimal("30"),"月饼","零食"); Productprod4=newProduct(4L,3,newBigDecimal("10"),"青岛啤酒","啤酒"); Productprod5=newProduct(5L,10,newBigDecimal("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中怎么正确高效的使用并行流
-
Java8如何使用CompletableFuture构建异步应用方式
-
基于Java8并行流有哪些需要注意的地方
这篇文章主要介绍了基于Java8并行流有哪些需要注意的地方,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大...
-
Java8中怎么将Array转换为Stream
本篇文章给大家分享的是有关Java8中怎么将Array转换为Stream,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇...
-
Java8中有哪些常用的时间api
这篇文章给大家介绍Java8中有哪些常用的时间api,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。Insta...
-
如何在Java 8中引入lambda表达式
本篇文章给大家分享的是有关如何在Java8中引入lambda表达式,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章...
-
怎么在Java8怎么在Map中新增数据
这期内容当中小编将会给大家带来有关怎么在Java8怎么在Map中新增数据,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文...
-
怎么在java8项目中对List对象属性去重
怎么在java8项目中对List对象属性去重?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面...
-
怎么在Java8中给forEach()函数提供index值
怎么在Java8中给forEach()函数提供index值?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细...
-
DateTimeFormatter与SimpleDateFormat在Java8中有什么区别
今天就跟大家聊聊有关DateTimeFormatter与SimpleDateFormat在Java8中有什么区别,可能很多人都不太...