• java8---1、为什么要用lambda表达式?


    java8---1、为什么要用lambda表达式?

    定义:函数作为参数传递到方法中。用一个表达式实现一个接口

    需求一:过滤商品价格超过10元的商品

    // 伪代码
    // 商品类
    class Goods{
       // 价格
       int price;
       // 生产日期
       Date productDate;
       // public  getter  settter  tostring
    }
    
    List<Student> goodsList=  new ArrayList();
    goodsList.add(new Goods(10,"20200415"));
    goodsList.add(new Goods(20,"20210415"));
    goodsList.add(new Goods(30,"20220415"));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    // 伪代码
    // 过滤商品的方法
    public List<Goods> fliterGoods(List<Goods> GoodsList){
       List<Goods> GoodsListFilter = new ArrayList();
       
       for(Goods goods : GoodsList){
          if(goods.getPrice>10){
              GoodsListFilter.add(goods) 
          }
       }
    }
    fliterGoods(goodsList)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    需求二:过滤商品价格超过15元的商品

    //  粘一份一摸一样的代码    只是判断条件给改了
    // 过滤商品的方法
    public List<Goods> fliterGoods(List<Goods> GoodsList){
       List<Goods> GoodsListFilter = new ArrayList();
       
       for(Goods goods : GoodsList){
          if(goods.getPrice>15){
              GoodsListFilter.add(goods) 
          }
       }
    }
    fliterGoods(goodsList)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    需求三:过滤商品价格超过20元的商品

    //  粘一份一摸一样的代码    只是判断条件给改了
    // 过滤商品的方法
    public List<Goods> fliterGoods(List<Goods> GoodsList){
       List<Goods> GoodsListFilter = new ArrayList();
       
       for(Goods goods : GoodsList){
          if(goods.getPrice>20){
              GoodsListFilter.add(goods) 
          }
       }
    }
    fliterGoods(goodsList)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    如果说这个时候还有新的需求,代码只能粘一份出来,且代码复用的太多了

    解决方式一: 使用接口减少代码复用

    需求一:过滤商品价格超过10元的商品

    // 接口文件
    public interface filterGoods{
        public Boolean fliter(Goods good)
    }
    
    • 1
    • 2
    • 3
    • 4
    // 接口的实现类
    class filterGoodsImpl implements  filterGoods{
       @override
        public Boolean fliter(Goods good){
            return goods.getPrice>10
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    // 过滤的方法
    public List<Goods> fliterGoods(List<Goods> GoodsList,filterGoods myfilterInterface){
       List<Goods> GoodsListFilter = new ArrayList();
       
       for(Goods goods : GoodsList){
          if(myfilterInterface.fliter){
              GoodsListFilter.add(goods) 
          }
       }
    }
    // 方法使用
    fliterGoods(GoodsList,new filterGoodsImpl())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    需求二:过滤商品价格超过15元的商品

    // 接口的实现类
    class filterGoodsImpl implements  filterGoods{
       @override
        public Boolean fliter(Goods good){
            return goods.getPrice>15
        }
    }
    // 方法调用
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    需求三:过滤商品价格超过20元的商品

    // 接口的实现类
    class filterGoodsImpl implements  filterGoods{
       @override
        public Boolean fliter(Goods good){
            return goods.getPrice>20
        }
    }
    // 方法调用
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    解决方式二: 使用匿名内部类接口文件

    // 接口文件
    public interface filterGoods{
        public Boolean fliter(Goods good)
    }
    
    • 1
    • 2
    • 3
    • 4
    // 过滤的方法
    public List<Goods> fliterGoods(List<Goods> GoodsList,filterGoods myfilterInterface){
       List<Goods> GoodsListFilter = new ArrayList();
       
       for(Goods goods : GoodsList){
          if(myfilterInterface.fliter){
              GoodsListFilter.add(goods) 
          }
       }
    }
    
    // 方法使用----------过滤商品价格超过10元的商品
    fliterGoods(GoodsList,new filterGoods(){
        @override
        public Boolean fliter(Goods good){
            return goods.getPrice>10
        }
    })
    
    // 方法使用----------过滤商品价格超过15元的商品
    fliterGoods(GoodsList,new filterGoods(){
        @override
        public Boolean fliter(Goods good){
            return goods.getPrice>15
        }
    })
    
    // 方法使用----------过滤商品价格超过20元的商品
    fliterGoods(GoodsList,new filterGoods(){
        @override
        public Boolean fliter(Goods good){
            return goods.getPrice>20
        }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    解决方式三: 使用lambda简化书写

    // 接口文件
    public interface filterGoods{
        public Boolean fliter(Goods good)
    }
    
    • 1
    • 2
    • 3
    • 4
    // 过滤的方法
    public List<Goods> fliterGoods(List<Goods> GoodsList,filterGoods myfilterInterface){
       List<Goods> GoodsListFilter = new ArrayList();
       
       for(Goods goods : GoodsList){
          if(myfilterInterface.fliter){
              GoodsListFilter.add(goods) 
          }
       }
    }
    
    // 方法使用----------过滤商品价格超过10元的商品
    fliterGoods(GoodsList,(good)-> good.getPrice()>10)
    
    // 方法使用----------过滤商品价格超过15元的商品
    fliterGoods(GoodsList,(good)-> good.getPrice()>15)
    
    // 方法使用----------过滤商品价格超过20元的商品
    fliterGoods(GoodsList,(good)-> good.getPrice()>20)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    是不是相当哇塞!!

    下面写一个例子demo

    // 伪代码,写一个lambda,对字符串进行任意操作(转小写,截取,转大写等)
    // 1 写一个接口
    interface lambdaStr{
       public String operateStr(String str);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    // 2 写处理字符串的方法
    public String handle(String str,lambdaStr mylambdastr){
       return mylambdastr.operateStr(str)
    }
    
    • 1
    • 2
    • 3
    • 4
    // 3 调用方法,写lambda
    handle("aaaa",(x)->{
      // 这里写对字符串的操作(转小写,截取,转大写等)
      return  x+"1"
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    QService 服务 指令引用的“0x00000000”内存。该内存不能为“read“
    万维网:蒂姆·伯纳斯·李的信息帝国
    day18 代码回想录 二叉树05 找树左下角的值&路径总和&从中序与后序遍历序列构造二叉树
    使用QLoRA对Llama 2进行微调的详细笔记
    如何学习存储性系统
    BRLWON Cobra
    抖音获得抖音商品详情 API 返回值说明
    软考 - 计算机组成与结构
    小bugs搜集和解决方法,亲测有效(2022-2023)
    数据中心电力运维,这次真包教包会!
  • 原文地址:https://blog.csdn.net/sugerfle/article/details/126498732