1.1 重点
1.2 代码编程
1.1 重点
一种新的思维方式:可以看看 demo(Predicate谓词接口,不建议大批量使用这种方法,容易类爆炸)
1.2 代码编程
需求:
传入任一参数 ,可以得到相应的查询结果(编程中只写了 商品种类和总价,其他的要加的话,直接扩展谓词接口实现类即可)
商品实体类:(同2-3)
枚举类:(同2-3)
商品服务类:(看filterSkuListByParamPrediCate方法)
package com.imooc.zhangxiaoxi.lambda.cart;
import java.util.ArrayList;
import java.util.List;
public class CartService {
/**
* 匿名内部类,有内存泄漏的风险,不在生产项目中引用,只是为了后边引用出来lambda表达式
*/
private static List<Sku> skuList = new ArrayList<Sku>(){
{
add(new Sku(2020001,"无人机",999.00,1,999.00,SkuCategoryEnum.ELECTRONICS));
add(new Sku(2020002,"T-shirt",50.00,2,100.00,SkuCategoryEnum.CLOTHING));
add(new Sku(2020003,"人生的枷锁",30.00,1,30.00,SkuCategoryEnum.BOOKS));
add(new Sku(2020004,"老人与海",20.00,1,20.00,SkuCategoryEnum.BOOKS));
add(new Sku(2020005,"剑指高效编程",288.00,1,288.00,SkuCategoryEnum.BOOKS));
add(new Sku(2020006,"大头皮鞋",300.00,1,300.00,SkuCategoryEnum.CLOTHING));
add(new Sku(2020007,"杠铃",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
* 找出购物车中所有电子产品
*
* 注意for 循环
* 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
* 单一维度条件参数化,传入不同的enum,打印出不同的产品列表
*
* 注意第二个参数:如何调取枚举类参数
*
*
* @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
* 然并卵
*
* 一个方法 可以 根据两个参数 任一参数判断,
*
* 若flag为true ,根据枚举类判断,否则,根据总价判断
*
* @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
* 生产上不建议这种写法,会引起类爆炸。一切都是为了引入lambda表达式
*
* @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;
}
}
谓词接口:
package com.imooc.zhangxiaoxi.lambda.cart; /** * 谓词接口 */ public interface SkuPrediCate { boolean test(Sku sku); }
谓词实现商品总价类:
package com.imooc.zhangxiaoxi.lambda.cart; public class TotalPricePrediCate implements SkuPrediCate { @Override public boolean test(Sku sku) { return sku.getTotalPrice()>200.00?true:false; } }
谓词实现商品种类类:
package com.imooc.zhangxiaoxi.lambda.cart; public class CategoryPrediCate implements SkuPrediCate { @Override public boolean test(Sku sku) { return SkuCategoryEnum.BOOKS.equals(sku.getSkuCategory())?true:false; } }
测试类:
package com.imooc.zhangxiaoxi.lambda.cart; import com.alibaba.fastjson.JSON; import org.junit.Test; import java.util.List; public class VersionTestD { @Test public void filterSkuListByParam(){ List<Sku> skuList = CartService.getSkuList(); /*List<Sku> getParamSkuList = CartService.filterSkuListByParamPrediCate(skuList,new TotalPricePrediCate()); System.out.println(JSON.toJSONString(getParamSkuList,true));*/ List<Sku> getParamSkuList = CartService.filterSkuListByParamPrediCate(skuList,new CategoryPrediCate()); System.out.println(JSON.toJSONString(getParamSkuList,true)); } }
打印日志:
[ { "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 } ] Process finished with exit code 0
诸葛