
Lambda表达式
2020-12-19 / highPhone啊
lambda表达式是JDK1.8的新特性之一,使用lambda表达式可以缩减大量的匿名内部类的使用,避免了大量重复冗余的编码,使代码变得简洁优雅,用于多线程、集合操作中可以极大的简化代码。表达式结构如下:
1 | (params) -> expression |
如上 () 是参数列表 {} 是方法体 -> 是lambda运算符
函数式接口
定义:函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口
1 |
|
lambda表达式的推导过程
在最初的接口中,我们要是实现接口方法,需要编写接口的实现类,再通过实现类重写方法来实现,例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22public class LambdaTest {
public static void main(String[] args) {
MyFunctionInterface1 lambda1 = new HelloLambda();
lambda1.sayHello();
}
}
class HelloLambda implements MyFunctionInterface1
{
public void sayHello() {
System.out.println("Hello Lambda.");
}
}
interface MyFunctionInterface1
{
//无参接口方法
void sayHello();
}如果接口的实现类是外部类,实现起来比较麻烦,一种改进的方法是把函数式接口的实现写到静态内部类中,如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22public class LambdaTest {
static class HelloLambda2 implements MyFunctionInterface1
{
public void sayHello() {
System.out.println("Hello Lambda2.");
}
}
public static void main(String[] args) {
MyFunctionInterface1 lambda2 = new HelloLambda2();
lambda2.sayHello();
}
}
interface MyFunctionInterface1
{
//无参接口方法
void sayHello();
}再进一步简化,可以使用局部内部类实现函数式接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22public class LambdaTest {
public static void main(String[] args) {
class HelloLambda3 implements MyFunctionInterface1
{
public void sayHello() {
System.out.println("Hello Lambda3.");
}
}
MyFunctionInterface1 lambda3 = new HelloLambda3();
lambda3.sayHello();
}
}
interface MyFunctionInterface1
{
//无参接口方法
void sayHello();
}再进一步,可以使用匿名内部类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19public class LambdaTest {
public static void main(String[] args) {
MyFunctionInterface1 lambda4 = new MyFunctionInterface1() {
public void sayHello() {
System.out.println("Hello Lambda4.");
}
};
lambda4.sayHello();
}
}
interface MyFunctionInterface1
{
//无参接口方法
void sayHello();
}在匿名内部类的基础上再进一步简化,就是我们的Lambda表达式了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public class LambdaTest {
public static void main(String[] args) {
MyFunctionInterface1 lambda5 = () ->{
System.out.println("Hello Lambda5.");
};
lambda5.sayHello();
}
}
interface MyFunctionInterface1
{
//无参接口方法
void sayHello();
}
Lambda表达式使用及简化
lambda表达式可以带参数及带参数类型,如下有个函数式接口定义了一个两个int类型参数及int类型返回的接口方法,我们用lambda表达式实现的calc方法作用是返回x+1+y的结果;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18public class LambdaTest {
public static void main(String[] args) {
MyFunctionInterface2 lambda2 = (int x, int y) -> {
x++;
return x+y;
};
System.out.println(lambda2.calc(10,5));
}
}
interface MyFunctionInterface2
{
//无参接口方法
int calc(int x, int y);
}
如上例子的lambda表达式中,有如下简化规则
- 参数类型可以省略,lambda表达式会自动按顺序匹配参数类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18public class LambdaTest {
public static void main(String[] args) {
MyFunctionInterface2 lambda2 = (x, y) -> {
x++;
return x+y;
};
System.out.println(lambda2.calc(10,5));
}
}
interface MyFunctionInterface2
{
//无参接口方法
int calc(int x, int y);
} - 如果函数式接口中接口方法的参数只有一个,则小括号可以省略
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18public class LambdaTest {
public static void main(String[] args) {
MyFunctionInterface2 lambda2 = x -> {
x += 10;
return x/5;
};
System.out.println(lambda2.calc(10));
}
}
interface MyFunctionInterface2
{
//无参接口方法
int calc(int x);
} - 如果lambda表达式实现的方法体中,只有一句表达式,或者一行语句,花括号也可以省略:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public class LambdaTest {
public static void main(String[] args) {
MyFunctionInterface2 lambda2 = x -> x/5;
System.out.println(lambda2.calc(10));
}
}
interface MyFunctionInterface2
{
//无参接口方法
int calc(int x);
}总结
一开始接触lambda表达式可能会看不习惯,但是就和三元表达式一样,使用lambda表达式有时候可以让我们的代码更加简洁和优雅。
- 参数类型可以省略,lambda表达式会自动按顺序匹配参数类型
本文链接:https://highphone.xyz/bbcf5ff7.html