多线程详解03: Lambda表达式 为什么要使用Lambda表达式 Functional Interface(函数式接口) 推导Lambda表达式 Lambda表达式简化过程

多线程详解03: Lambda表达式

Lambda表达式:

  1. λ是希腊字母表中第十一位字母,英文名称Lambda.
  2. 使用Lambda表达式是为了避免匿名内部类定义过多.
  3. Lambda表达式的实质是函数式编程.
  4. 通用形式为:
(params参数) -> expression[表达式]
(params参数) -> statement[语句]
(params参数) -> {statements}

为什么要使用Lambda表达式

  1. 避免内部类定义过多
  2. 提高代码简洁性,去掉了一堆没有意义的代码,只留下核心逻辑.

Functional Interface(函数式接口)

  1. 理解Functional Interface(函数式接口)是学习JAVA8 lambda表达式的关键所在.
  2. 函数式接口的定义: 任何接口,如果只包含唯一一个抽象方法,那么它就是函数式接口.
  3. 对于函数式接口,我们可以通过lambda表达式来创建该接口的对象.

推导Lambda表达式

下面我们通过代码来推导一下lambda表达式.

/**
 * @ClassName LambdaTest1
 * @Author clap of thunder
 * @Date 2021/9/22 16:36
 * @Description 推导lambda表达式
 **/
public class LambdaTest1 {
    public static void main(String[] args) {
        Interface ifi = new InterfaceImpl();
        ifi.interMethod();

        ifi = new InterfaceImpl2();
        ifi.interMethod();

        /**4.局部内部类:方法内部建类*/
        class InterfaceImpl3 implements Interface{
            @Override
            public void interMethod() {
                System.out.println("lambda3");
            }
        }
        ifi = new InterfaceImpl3();
        ifi.interMethod();

        /**5.匿名内部类:没有类的名称,必须借助接口或父类*/
        ifi = new Interface() {
            @Override//有父类所以也要重写方法
            public void interMethod() {
                System.out.println("lambda4");

            }
        };
        ifi.interMethod();

        /**6.JDK8: Lambda表达式:函数式接口只有一个方法,所以可以直接简化*/
        ifi = () ->{
            System.out.println("lambda5");
        };
        ifi.interMethod();


    }

    /**3.静态内部类
     * @author: clap of thunder
     * @date:  2021/9/22 17:33
     */
    static class InterfaceImpl2 implements Interface{
        @Override
        public void interMethod() {
            System.out.println("lambda2");
        }
    }

}

/** 1.定义一个函数式接口*/
interface Interface{
    /**
     * interMethod is good!
     * @author: clap of thunder
     * @date:  2021/9/22 17:23
     * @return null
     */
    void interMethod();

}

/** 2.实现类*/
class InterfaceImpl implements Interface{
    @Override
    public void interMethod() {
        System.out.println("lambda1");
    }
}

Lambda表达式简化过程

上面的代码让我们看清了推导过程,但是关于Lambda表达式到底是怎样提升代码简洁性的并没有很清楚地说明.现在让我们通过下面的实例来具体感悟一下:

/**
 * @author clap of thunder
 * @date 2021-09-22 18:15
 */
public class LambdaTest2 {
    public static void main(String[] args) {
        Money money = new Money() {
            @Override
            public void makeMoney(int a, int b, int c) {
                System.out.println("I have " + a+b+c + " yuan");
            }
        };
            money.makeMoney(1,2,3 );
            
        }
    }

    interface Money {
        void makeMoney(int a, int b, int c);

    }

简化步骤1:使用Lambda表达式

public class LambdaTest2 {
    public static void main(String[] args) {
    //简化步骤1:使用Lambda表达式
      money = (int a, int b, int c) -> {
                System.out.println("I have " + a+b+c + " yuan");
            };
            money.makeMoney(1,3,2);
            
        }
    }

    interface Money {
        void makeMoney(int a, int b, int c);

    }


简化步骤2:Lambda去掉参数类型

   public class LambdaTest2 {
    public static void main(String[] args) {
    //简化步骤2:Lambda去掉参数类型
    money = (int a, int b, int c) -> {
               System.out.println("I have " + a + " yuan");
            };
            money.makeMoney(2,1,3);
            
        }
    }

    interface Money {
        void makeMoney(int a, int b, int c);

    }

   
  
    

简化步骤3: Lambda去掉小括号类型:只能用于只有单个参数时

 friends =(a)-> {System.out.println("I have "+a+" makeMoney");};
            friends.makeFriends(3);
            
 friends = a -> System.out.println("I have "+a+" makeMoney");
            friends.makeFriends(4);

步骤3的代码来源于下面的示例:

/**
 * @author clap of thunder
 * @date 2021-09-22 18:40
 * @description
 */
public class LambdaTest3 {
        public static void main(String[] args) {

            //匿名内部类
            Friends friends = new Friends() {
                @Override
                public void makeFriends(int a) {
                    System.out.println("I have "+a+" makeMoney");
                }
            };
            friends.makeFriends(1);
            //简化1:Lambda
            friends =(int a)-> {System.out.println("I have "+a+" makeMoney");};
            friends.makeFriends(2);
            //简化2:Lambda去掉参数类型
            friends =(a)-> {System.out.println("I have "+a+" makeMoney");};
            friends.makeFriends(3);
            //简化2:Lambda去掉小括号类型:只能用于只有单个参数时
            friends = a -> System.out.println("I have "+a+" makeMoney");
            friends.makeFriends(4);

            //总结:1.Lambda表达式只能在只有一行的情况下才能简化为一行,如果有多行就要用代码块包裹
            //2.前提是接口为函数式接口
            //3.多个参数也可以去掉参数类型但是去掉就必须加上括号


        }
    }

    interface Friends {
        /**
         * just a test
         * @author: clap of thunder
         * @date:  2021/9/22 18:19
         * @param a  for test.
         * @return null
         */
        void makeFriends(int a);

    }

总结:

  1. Lambda表达式只能在只有一行的情况下才能简化为一行,如果有多行就要用代码块包裹

  2. 前提是接口为函数式接口

  3. 多个参数也可以去掉参数类型但是去掉就必须加上括号

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Clap of thunder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值