Java8函数式编程之Stream编程详解(4)收集器
本文我们重点讲解一下Stream中收集器Collector的主要使用方式。
收集器
收集器(Collector)的作用为:将流中元素累积成一个结果作用于终端操作collect()上
关于收集器,我们主要关注:
collect()(操作实现Collector接口的集合)、
Collector(接口)
Collectors(工具类)
日常开发中,我们使用最多的是预定义收集器功能(Collectors)
它的作用主要是:
将流元素规约和汇总为一个值
将流元素分组
将流元素分区
Collect()方法解析
<R> R collect(Supplier<R> supplier, 初始化结果容器
BiConsumer<R, ? super T> accumulator, 添加元素到结果容器的逻辑
BiConsumer<R, R> combiner); 并行执行时多个结果容器的合并方式
我们此处介绍几个常用的预定义收集器方法
集合收集器
@Test
public void toList() {
List<Sku> list = CartService.getCartSkuList();
List<Sku> result = list.stream()
.filter(sku -> sku.getTotalPrice() > 100)
.collect(Collectors.toList());
System.out.println(JSON.toJSONString(result, true));
}
这个用例的目的是:选出总价大于100的商品并打印,也就是大于100的sku会被保留。
运行结果:
[
{
"skuCategory":"ELECTRONICS",
"skuId":2,
"skuName":"无人机",
"skuPrice":1000.0,
"totalNum":10,
"totalPrice":1000.0
},
{
"skuCategory":"ELECTRONICS",
"skuId":1,
"skuName":"VR一体机",
"skuPrice":2100.0,
"totalNum":10,
"totalPrice":2100.0
},
{
"skuCategory":"CLOTHING",
"skuId":13,
"skuName":"衬衫",
"skuPrice":120.0,
"totalNum":10,
"totalPrice":120.0
}
]
Process finished with exit code 0
集合分组
@Test
public void group() {
List<Sku> list = CartService.getCartSkuList();
// key=分组条件 value=元素集合 即Map<分组条件,结果集合>
Map<Enum, List<Sku>> result = list.stream()
.collect(Collectors.groupingBy(sku -> sku.getSkuCategory()));
System.out.println(JSON.toJSONString(result, true));
}
这个用例的目的是:根据sku类别对list进行分组,分组结束后返回一个Map<分组条件,结果集合>,即key=分组条件 value=元素集合
运行结果:
{"CLOTHING":[
{
"skuCategory":"CLOTHING",
"skuId":4,
"skuName":"牛仔裤",
"skuPrice":60.0,
"totalNum":10,
"totalPrice":60.0
},
{
"skuCategory":"CLOTHING",
"skuId":13,
"skuName":"衬衫",
"skuPrice":120.0,
"totalNum":10,
"totalPrice":120.0
}
],"BOOKS":[
{
"skuCategory":"BOOKS",
"skuId":121,
"skuName":"Java编程思想",
"skuPrice":100.0,
"totalNum":10,
"totalPrice":100.0
},
{
"skuCategory":"BOOKS",
"skuId":3,
"skuName":"程序化广告",
"skuPrice":80.0,
"totalNum":10,
"totalPrice":80.0
}
],"ELECTRONICS":[
{
"skuCategory":"ELECTRONICS",
"skuId":2,
"skuName":"无人机",
"skuPrice":1000.0,
"totalNum":10,
"totalPrice":1000.0
},
{
"skuCategory":"ELECTRONICS",
"skuId":1,
"skuName":"VR一体机",
"skuPrice":2100.0,
"totalNum":10,
"totalPrice":2100.0
}
]
}
从运行结果我们可以看出满足要求。
集合分区
集合分区是分组的一种特例:
分区是由一个谓词作为分区函数,分区函数返回一个boolean值最终将分区结果分为两组,一组为boolean=true的 一组为boolean=false的通俗的说也就是满足条件的分为一组,不满足条件的为一组
@Test
public void partition() {
List<Sku> list = CartService.getCartSkuList();
Map<Boolean, List<Sku>> partition = list.stream()
.collect(Collectors.partitioningBy(sku -> sku.getTotalPrice() > 100));
System.out.println(JSON.toJSONString(partition, true));
}
运行结果:
{
false:[
{
"skuCategory":"CLOTHING",
"skuId":4,
"skuName":"牛仔裤",
"skuPrice":60.0,
"totalNum":10,
"totalPrice":60.0
},
{
"skuCategory":"BOOKS",
"skuId":121,
"skuName":"Java编程思想",
"skuPrice":100.0,
"totalNum":10,
"totalPrice":100.0
},
{
"skuCategory":"BOOKS",
"skuId":3,
"skuName":"程序化广告",
"skuPrice":80.0,
"totalNum":10,
"totalPrice":80.0
}
],
true:[
{
"skuCategory":"ELECTRONICS",
"skuId":2,
"skuName":"无人机",
"skuPrice":1000.0,
"totalNum":10,
"totalPrice":1000.0
},
{
"skuCategory":"ELECTRONICS",
"skuId":1,
"skuName":"VR一体机",
"skuPrice":2100.0,
"totalNum":10,
"totalPrice":2100.0
},
{
"skuCategory":"CLOTHING",
"skuId":13,
"skuName":"衬衫",
"skuPrice":120.0,
"totalNum":10,
"totalPrice":120.0
}
]
}
Process finished with exit code 0
通过日志打印我们能够看出,根据是否满足断言将集合分为两个组。结果与分组很像,只不过key变成了true/false。
版权声明:
原创不易,洗文可耻。除非注明,本博文章均为原创,转载请以链接形式标明本文地址。