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