多线程详解03: Lambda表达式
Lambda表达式:
- λ是希腊字母表中第十一位字母,英文名称Lambda.
- 使用Lambda表达式是为了避免匿名内部类定义过多.
- Lambda表达式的实质是函数式编程.
- 通用形式为:
(params参数) -> expression[表达式]
(params参数) -> statement[语句]
(params参数) -> {statements}
为什么要使用Lambda表达式
- 避免内部类定义过多
- 提高代码简洁性,去掉了一堆没有意义的代码,只留下核心逻辑.
Functional Interface(函数式接口)
- 理解Functional Interface(函数式接口)是学习JAVA8 lambda表达式的关键所在.
- 函数式接口的定义: 任何接口,如果只包含唯一一个抽象方法,那么它就是函数式接口.
- 对于函数式接口,我们可以通过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);
}
总结:
-
Lambda表达式只能在只有一行的情况下才能简化为一行,如果有多行就要用代码块包裹
-
前提是接口为函数式接口
-
多个参数也可以去掉参数类型但是去掉就必须加上括号