1.1 重点
1.2 代码编程
1.1 重点
注意:jdk8已上才能用lambda表达式
本节简单介绍了lambda表达式的使用,具体的使用案例参照2-15
使用了lambda表达式,请参考测试类 VerSionTestF
核心思想:把函数作为参数传递给另一个函数
1.2 代码编程
商品实体类:(同2-3)
枚举类:(同2-3)
商品服务类:
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; import com.alibaba.fastjson.JSON; import org.junit.Test; import java.util.List; public class VersionTestF { @Test public void filterSkuListByParam(){ List<Sku> skuList = CartService.getSkuList(); /** * 使用lambda表达式 查询 总价 大于1000的
* 这里lambda相当于写实现了,需要用到sku的getTotalPrice方法,所以入参sku(自己理解的) */ List<Sku> getParamSkuList = CartService.filterSkuListByParamPrediCate(skuList, (Sku sku) -> sku.getTotalPrice() > 1000); System.out.println(JSON.toJSONString(getParamSkuList,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
诸葛