1.1  重点

1.2  代码编程

 

 

1.1  重点

1  使用了匿名内部类,但是关于内存泄漏风险的解决一直没有明显的方案,这个后边小喜老师 会@我的,持续关注下即可

2  Lambda是不会引起内存泄漏的。原因就是从底层屏蔽掉了由于内部类持有外部对象所造成的内存泄漏问题。放心大胆的用就好了。

关于匿名类内存泄漏的问题,主要抓住本质:内部类持有外部对象引用造成无形中延长了外部对象的生命周期,而形成的。

 

 

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 VersionTestE {

    @Test
   public void filterSkuListByParam(){
       List<Sku> skuList = CartService.getSkuList();

        /**
         * 使用匿名内部类 查询 总价 大于1000的
         */
        List<Sku> getParamSkuList = CartService.filterSkuListByParamPrediCate(skuList, new SkuPrediCate() {
            @Override
            public boolean test(Sku sku) {
                return 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

 

posted on 2020-04-03 06:11  菜鸟乙  阅读(214)  评论(0编辑  收藏  举报