Lambda表达式介绍

Lambda是JDK8的新特性之一,提供了更简洁的代码写法。下面先来看看Lambda表达式是怎么样的

定义一个接口并定义一个抽象方法:

package com.huajieyu.code.y2024.sept.at0915c;

public interface ExampleA {

    void run();
}

下面举个例子,创建对象执行一下

package com.huajieyu.code.y2024.sept.at0915c;

public class ExampleATest {

    public static void main(String[] args) {
        test1();
        test2();
    }

    public static void test1(){
        ExampleA example = new ExampleA() {
            @Override
            public void run() {
                System.out.println("这是常规写法");
            }
        };

        // 调用run方法
        example.run();
    }

    public static void test2(){
        ExampleA example = () -> {
            System.out.println("Lambda表达式的写法");
        };

        // 调用run方法
        example.run();
    }
}

控制台输出如下:

这是常规写法
Lambda表达式的写法

可以看到都被正常输出了。对于只有一个需要实现的方法的接口来说,这种接口就叫函数式接口。Lambda表达式是针对函数式接口进行的拓展,具体实现方式需要根据抽象方法的定义来实现。

比如ExampleA里的run方法,并没有参数,而且没有返回值,则可以把写一个空括号代表参数列表,然后->指向方法体,方法体内部可以写具体的自定义逻辑。

ExampleA example = () -> {
    System.out.println("Lambda表达式的写法");
};

执行的时候通过example调run方法即可。

无参无返回值的写法;

package com.huajieyu.code.y2024.sept.at0915c;

public interface ExampleA {

    void run();
}

package com.huajieyu.code.y2024.sept.at0915c;

public class ExampleATest {

    public static void main(String[] args) {
        test();
    }

    public static void test(){
        ExampleA example = () -> {
            System.out.println("无参无返回");
        };

        // 调用run方法
        example.run();
    }
}

效果如下:

无参无返回

Process finished with exit code 0

有一个参数无返回值的写法:

package com.huajieyu.code.y2024.sept.at0915c;

public interface ExampleB {

    void run(String name);
}
package com.huajieyu.code.y2024.sept.at0915c;

public class ExampleBTest {

    public static void main(String[] args) {
        test1();
    }

    public static void test1(){
        ExampleB example = (e) -> {
            System.out.println("My name is " + e);
        };

        // 调用run方法
        example.run("Tom");
        example.run("Rosy");
    }
}

括号内的e就是String类型的name的别名,在方法体内部可以调用。

效果如下;

My name is Tom
My name is Rosy

Process finished with exit code 0

另外还有一种写法,当参数只有一个的时候,可以把括号省略掉,变成下面的样子

package com.huajieyu.code.y2024.sept.at0915c;

public class ExampleBTest {

    public static void main(String[] args) {
        test1();
    }

    public static void test1() {
        ExampleB example = e -> {
            System.out.println("My name is " + e);
        };
        // 调用run方法
        example.run("Tom");
        example.run("Rosy");
    }
}

而且如果方法体只有一句语句的话,花括号也可以省略掉,写成下面这个样子:

package com.huajieyu.code.y2024.sept.at0915c;

public class ExampleBTest {

    public static void main(String[] args) {
        test1();
    }

    public static void test1(){
        ExampleB example = ExampleB example = e -> System.out.println("My name is " + e);

        // 调用run方法
        example.run("Tom");
        example.run("Rosy");
    }
}

还有更简洁的方法,这种写法称为方法引用(这种的话需要把参数提前设好,因为不能加上其他处理逻辑,比如字符串拼接):

package com.huajieyu.code.y2024.sept.at0915c;

public class ExampleBTest {

    public static void main(String[] args) {
        test1();
    }

    public static void test1(){
        ExampleB example = System.out::println;

        // 调用run方法
        example.run("Tom");
        example.run("Rosy");
    }
}

有多个参数无返回值的写法

package com.huajieyu.code.y2024.sept.at0915c;

public class ExampleCTest {

    public static void main(String[] args) {
        test1();
    }

    public static void test1(){
        ExampleC example = (a, b) -> System.out.println("My name is " + a + ", I'm " + b + " years old");

        // 调用run方法
        example.run("John", 18);
        example.run("Russ", 17);
    }
}

需要用括号把多个参数都括起来,多个参数的情况下括号不能被省略,另外如果有多条语句的话,花括号也是不可省略的。

效果如下:

My name is John, I'm 18 years old
My name is Russ, I'm 17 years old

Process finished with exit code 0

无参有返回值的写法

package com.huajieyu.code.y2024.sept.at0915c;

public interface ExampleD {

    int run();
}
package com.huajieyu.code.y2024.sept.at0915c;

public class ExampleDTest {

    public static void main(String[] args) {
        test1();
    }

    public static void test1(){
        ExampleD example = () -> {
            return 1024;
        };

        // 调用run方法
        System.out.println(example.run());
    }
}

效果如下

1024

Process finished with exit code 0

对于只有一句return语句的,可以把花括号和return关键字同时省略掉,如下:

package com.huajieyu.code.y2024.sept.at0915c;

public class ExampleDTest {

    public static void main(String[] args) {
        test1();
    }

    public static void test1(){
        ExampleD example = () -> 1024;

        // 调用run方法
        System.out.println(example.run());
    }
}

有一个参数有返回值的写法

package com.huajieyu.code.y2024.sept.at0915c;

public interface ExampleE {

    int run(int num);
}
package com.huajieyu.code.y2024.sept.at0915c;

public class ExampleETest {

    public static void main(String[] args) {
        test1();
    }

    public static void test1(){
        ExampleE example = e -> e * 2;

        // 调用run方法
        System.out.println(example.run(24));
    }
}

预想是输出24的2倍,也就是48,效果如下;

1024

Process finished with exit code 0

有多个参数有返回值的写法

package com.huajieyu.code.y2024.sept.at0915c;

public interface ExampleF {

    int run(int num1, int num2);
}
package com.huajieyu.code.y2024.sept.at0915c;

public class ExampleFTest {

    public static void main(String[] args) {
        test1();
    }

    public static void test1(){
        ExampleF example = (n1, n2) -> n1 + n2;

        // 调用run方法
        System.out.println(example.run(1000, 24));
    }
}

这边会输出n1+n2的值,效果如下:

1024

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值