当前位置 > it书童 > java > 正文

流的收集器

java it书童 2021-01-14 16:20:41 0赞 0踩 485阅读 0评论

将流中的元素累积成一个结果,作用于终端操作 collect() 上

预定义收集器功能:

将流元素归约和汇总为一个值

package stream;

import com.alibaba.fastjson.JSON;
import lambda.cart.CartService;
import lambda.cart.Sku;
import org.junit.Test;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 常见的预定义收集器使用
 */
public class StreamCollector {

    public List<Sku> list = CartService.getCartSkuList();

    /**
     * 集合收集器
     */
    @Test
    public void toList() {
        List<Sku> result = list.stream()
            .filter(sku -> sku.getTotalPrice() > 1000)
            .collect(Collectors.toList());
        System.out.println(JSON.toJSONString(result, true));
    }
}

结果:

[
	{
		"skuCategory":"ELECTRONICS",
		"skuId":654032,
		"skuName":"无人机",
		"skuPrice":4999.0,
		"totalNum":1,
		"totalPrice":4999.0
	},
	{
		"skuCategory":"ELECTRONICS",
		"skuId":642934,
		"skuName":"VR一体机",
		"skuPrice":2299.0,
		"totalNum":1,
		"totalPrice":2299.0
	},
	{
		"skuCategory":"CLOTHING",
		"skuId":645321,
		"skuName":"纯色衬衫",
		"skuPrice":409.0,
		"totalNum":3,
		"totalPrice":1227.0
	},
	{
		"skuCategory":"SPORTS",
		"skuId":675489,
		"skuName":"跑步机",
		"skuPrice":2699.0,
		"totalNum":1,
		"totalPrice":2699.0
	}
]

将流元素分组

/**
    * 分组
    */
@Test
public void group() {
    // Map<分组条件,结果集合>
    Map<Enum, List<Sku>> result = list.stream()
        .filter(sku -> sku.getTotalPrice() > 1000)
        .collect(Collectors.groupingBy(sku -> sku.getSkuCategory()));
    System.out.println(JSON.toJSONString(result, true));
}

结果:

{"CLOTHING":[
		{
			"skuCategory":"CLOTHING",
			"skuId":645321,
			"skuName":"纯色衬衫",
			"skuPrice":409.0,
			"totalNum":3,
			"totalPrice":1227.0
		}
	],"SPORTS":[
		{
			"skuCategory":"SPORTS",
			"skuId":675489,
			"skuName":"跑步机",
			"skuPrice":2699.0,
			"totalNum":1,
			"totalPrice":2699.0
		}
	],"ELECTRONICS":[
		{
			"skuCategory":"ELECTRONICS",
			"skuId":654032,
			"skuName":"无人机",
			"skuPrice":4999.0,
			"totalNum":1,
			"totalPrice":4999.0
		},
		{
			"skuCategory":"ELECTRONICS",
			"skuId":642934,
			"skuName":"VR一体机",
			"skuPrice":2299.0,
			"totalNum":1,
			"totalPrice":2299.0
		}
	]
}

将流元素分区

/**
    * 分区
    */
@Test
public void partition() {
    Map<Boolean, List<Sku>> result = list.stream()
        .collect(Collectors.partitioningBy(sku -> sku.getTotalPrice() > 1000));
    System.out.println(JSON.toJSONString(result, true));
}

结果:

{false:[
		{
			"skuCategory":"CLOTHING",
			"skuId":654327,
			"skuName":"牛仔裤",
			"skuPrice":528.0,
			"totalNum":1,
			"totalPrice":528.0
		},
		{
			"skuCategory":"BOOKS",
			"skuId":644564,
			"skuName":"Java编程思想",
			"skuPrice":79.8,
			"totalNum":1,
			"totalPrice":79.8
		},
		{
			"skuCategory":"BOOKS",
			"skuId":678678,
			"skuName":"Java核心技术",
			"skuPrice":149.0,
			"totalNum":1,
			"totalPrice":149.0
		},
		{
			"skuCategory":"BOOKS",
			"skuId":697894,
			"skuName":"算法",
			"skuPrice":78.2,
			"totalNum":1,
			"totalPrice":78.2
		},
		{
			"skuCategory":"BOOKS",
			"skuId":696968,
			"skuName":"TensorFlow进阶指南",
			"skuPrice":85.1,
			"totalNum":1,
			"totalPrice":85.1
		}
	],true:[
		{
			"skuCategory":"ELECTRONICS",
			"skuId":654032,
			"skuName":"无人机",
			"skuPrice":4999.0,
			"totalNum":1,
			"totalPrice":4999.0
		},
		{
			"skuCategory":"ELECTRONICS",
			"skuId":642934,
			"skuName":"VR一体机",
			"skuPrice":2299.0,
			"totalNum":1,
			"totalPrice":2299.0
		},
		{
			"skuCategory":"CLOTHING",
			"skuId":645321,
			"skuName":"纯色衬衫",
			"skuPrice":409.0,
			"totalNum":3,
			"totalPrice":1227.0
		},
		{
			"skuCategory":"SPORTS",
			"skuId":675489,
			"skuName":"跑步机",
			"skuPrice":2699.0,
			"totalNum":1,
			"totalPrice":2699.0
		}
	]
}
关于我
一个文科出身的程序员,追求做个有趣的人,传播有价值的知识,微信公众号主要分享读书思考心得,不会有代码类文章,非程序员的同学请放心订阅
转载须注明出处:https://www.itshutong.com/articles/1026