1.1 Key
1.2 Programming Code
1.1 Key
A new way of thinking: can see Demo ( a method can be one of the parameters is determined according to two parameters )
1.2 Programming Code
demand:
The method of any one of a parameter can be determined according to two parameters,
Commodity same entity class :( 2-3)
Goods class :( see filterSkuListByParamB method)
package com.imooc.zhangxiaoxi.lambda.cart; import java.util.ArrayList; import java.util.List; public class CartService { /** * Anonymous inner classes, there is the risk of memory leaks, not production projects referenced, only to back out of a lambda expression references */ private static List<Sku> skuList = new ArrayList<Sku>(){ { the Add ( new new Sku (2020001, "UAV", 999.00,1,999.00 , SkuCategoryEnum.ELECTRONICS)); add(new Sku(2020002,"T-shirt",50.00,2,100.00,SkuCategoryEnum.CLOTHING)); the Add ( new new Sku (2020003, "Of Human Bondage", 30.00,1,30.00 , SkuCategoryEnum.BOOKS)); the Add ( new new Sku (2020004, "Old Man", 20.00,1,20.00 , SkuCategoryEnum.BOOKS)); the Add ( new new Sku (2020005, "prove safety and efficient programming", 288.00,1,288.00 , SkuCategoryEnum.BOOKS)); the Add ( new new Sku (2020006, "bulk shoes", 300.00,1,300.00 , SkuCategoryEnum.CLOTHING)); the Add ( new new Sku (2020007, "barbell", 2000.00,1,2000.00 , SkuCategoryEnum.SPROTS)); add(new Sku(2020008,"ThinkPad",5000.00,1,5000.00,SkuCategoryEnum.ELECTRONICS)); } }; public static List<Sku> getSkuList(){ return skuList; } /** * versionTestA * Find out all electronic products in shopping cart * * Note for loop * For (Sku sku: skuList) 的 使用 * @return */ public static List<Sku> getElectronicsSkuList(List<Sku> skuList){ List<Sku> lElectronicsList = new ArrayList<Sku>(); for (Sku sku: skuList){ if (sku.getSkuCategory().equals(SkuCategoryEnum.ELECTRONICS)){ lElectronicsList.add (sku); } } /* for (int i=0;i<skuList.size();i++){ if (skuList.get(i).getSkuCategory().equals(SkuCategoryEnum.ELECTRONICS)){ lElectronicsList.add (skuList.get (i)); } } * / Return lElectronicsList; } /** * versionTestB * Condition parameters of a single dimension, passed different enum, print out a list of different products * * Note that the second argument: how the transfer of enumeration class parameters * * * @Param skuList * @return */ public static List<Sku> filterSkuListByParam(List<Sku> skuList,SkuCategoryEnum skuCategoryEnum){ List<Sku> lElectronicsList = new ArrayList<Sku>(); for (int i=0;i<skuList.size();i++){ if (skuList.get(i).getSkuCategory().equals(skuCategoryEnum)){ lElectronicsList.add (skuList.get (i)); } } return lElectronicsList; } /** * versionTestC * Ran and eggs * * A method may be any of the parameters is determined according to two parameters, * * If the flag is true, the judge enumerated class, otherwise, judgment based on the total price * * @Param skuList * @return */ public static List<Sku> filterSkuListByParamB(List<Sku> skuList,SkuCategoryEnum skuCategoryEnum,Double dTotalPrice,boolean flag){ List<Sku> lElectronicsList = new ArrayList<Sku>(); for (int i=0;i<skuList.size();i++){ if ((flag && skuList.get(i).getSkuCategory().equals(skuCategoryEnum)) || (!flag&&skuList.get(i).getTotalPrice()>dTotalPrice)){ lElectronicsList.add (skuList.get (i)); } } return lElectronicsList; } /** * versionTestD * The production does not recommend such an approach would lead to class explosion. Everything is in order to introduce a lambda expression * * @Param skuList * @return */ public static List<Sku> filterSkuListByParamPrediCate(List<Sku> skuList,SkuPrediCate skuPrediCate){ List<Sku> lElectronicsList = new ArrayList<Sku>(); for (int i=0;i<skuList.size();i++){ if (skuPrediCate.test(skuList.get(i))){ lElectronicsList.add (skuList.get (i)); } } return lElectronicsList; } }
:( same enumeration class 2-3)
Test categories:
package com.imooc.zhangxiaoxi.lambda.cart; import com.alibaba.fastjson.JSON; import org.junit.Test; import java.util.List; public class VersionTestC { @Test public void filterSkuListByParam(){ List<Sku> skuList = CartService.getSkuList(); /*List<Sku> getParamSkuList = CartService.filterSkuListByParamB(skuList,SkuCategoryEnum.BOOKS,null,true); System.out.println(JSON.toJSONString(getParamSkuList,true));*/ List<Sku> getParamSkuList = CartService.filterSkuListByParamB(skuList,null,200.00,false); System.out.println(JSON.toJSONString(getParamSkuList,true)); } }
Print log:
[ { "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