1 重点:
1.1 Collectors方法之 流转集合,分组,分区的案例应用
2 demo之流转集合(toCollectors):
demo:
/** * toList * 案例1:流转换为集合 * 注意:可以用Collectors.toList /toSet/ toMap 等转换成不同的集合 * */ @Test public void toListTest(){ List<Sku> list = CartService.getSkuList(); List<Sku> list2 = list.stream().filter(Sku->Sku.getTotalPrice()>1000).collect(Collectors.toList()); System.out.println(JSONObject.toJSONString(list2,true)); }
打印日志:
[ { "skuCategory":"SPROTS", "skuId":2020007, "skuName":"杠铃", "skuPrice":2000.0, "totalNum":1, "totalPrice":2000.0 }, { "skuCategory":"ELECTRONICS", "skuId":2020008, "skuName":"ThinkPad", "skuPrice":5000.0, "totalNum":1, "totalPrice":5000.0 } ] Process finished with exit code 0
3 demo之分组(groupingBy):
demo:
/** * groupingBy * 案例2:根据产品种类分组 * 注意:map的定义 */ @Test public void groupTest(){ List<Sku> list = CartService.getSkuList(); Map<Object,List<Sku>> map = list.stream().collect(Collectors.groupingBy(Sku::getSkuCategory)); System.out.println(JSONObject.toJSONString(map,true)); }
打印日志:
{"CLOTHING":[ { "skuCategory":"CLOTHING", "skuId":2020002, "skuName":"T-shirt", "skuPrice":50.0, "totalNum":2, "totalPrice":100.0 }, { "skuCategory":"CLOTHING", "skuId":2020006, "skuName":"大头皮鞋", "skuPrice":300.0, "totalNum":1, "totalPrice":300.0 } ],"BOOKS":[ { "skuCategory":"BOOKS", "skuId":2020003, "skuName":"人生的枷锁", "skuPrice":30.0, "totalNum":1, "totalPrice":30.0 }, { "skuCategory":"BOOKS", "skuId":2020004, "skuName":"老人与海", "skuPrice":20.0, "totalNum":1, "totalPrice":20.0 }, { "skuCategory":"BOOKS", "skuId":2020005, "skuName":"剑指高效编程", "skuPrice":288.0, "totalNum":1, "totalPrice":288.0 } ],"SPROTS":[ { "skuCategory":"SPROTS", "skuId":2020007, "skuName":"杠铃", "skuPrice":2000.0, "totalNum":1, "totalPrice":2000.0 } ],"ELECTRONICS":[ { "skuCategory":"ELECTRONICS", "skuId":2020001, "skuName":"无人机", "skuPrice":999.0, "totalNum":1, "totalPrice":999.0 }, { "skuCategory":"ELECTRONICS", "skuId":2020008, "skuName":"ThinkPad", "skuPrice":5000.0, "totalNum":1, "totalPrice":5000.0 } ] } Process finished with exit code 0
4 demo之分区(partitioningBy):
demo:
@Test /** * partitioningBy * 案例3:根据总价是否大于100进行分区 * 分区是分组的一种特殊形式,根据特定的条件为true或者为否分为两组 */ public void partitioningByTest(){ List<Sku> list = CartService.getSkuList(); Map<Boolean,List<Sku>> map = list.stream().collect(Collectors.partitioningBy(Sku->Sku.getTotalPrice()>100)); System.out.println(JSONObject.toJSONString(map,true)); }
打印日志:
{false:[ { "skuCategory":"CLOTHING", "skuId":2020002, "skuName":"T-shirt", "skuPrice":50.0, "totalNum":2, "totalPrice":100.0 }, { "skuCategory":"BOOKS", "skuId":2020003, "skuName":"人生的枷锁", "skuPrice":30.0, "totalNum":1, "totalPrice":30.0 }, { "skuCategory":"BOOKS", "skuId":2020004, "skuName":"老人与海", "skuPrice":20.0, "totalNum":1, "totalPrice":20.0 } ],true:[ { "skuCategory":"ELECTRONICS", "skuId":2020001, "skuName":"无人机", "skuPrice":999.0, "totalNum":1, "totalPrice":999.0 }, { "skuCategory":"BOOKS", "skuId":2020005, "skuName":"剑指高效编程", "skuPrice":288.0, "totalNum":1, "totalPrice":288.0 }, { "skuCategory":"CLOTHING", "skuId":2020006, "skuName":"大头皮鞋", "skuPrice":300.0, "totalNum":1, "totalPrice":300.0 }, { "skuCategory":"SPROTS", "skuId":2020007, "skuName":"杠铃", "skuPrice":2000.0, "totalNum":1, "totalPrice":2000.0 }, { "skuCategory":"ELECTRONICS", "skuId":2020008, "skuName":"ThinkPad", "skuPrice":5000.0, "totalNum":1, "totalPrice":5000.0 } ] } Process finished with exit code 0
诸葛