一、Lambda表达式简介
Lambda 表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性。
Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中)。
使用 Lambda 表达式可以使代码变的更加简洁紧凑。
二、Lambda基本语法
箭头操作符:() ->{}
Java8中引入了一个新的操作符,"->",该操作符称为箭头操作符或者Lambda操作符,箭头操作符将Lambda表达式拆分成两部分;
左侧: Lambda表达式的参数列表,对应的是接口中抽象方法的参数列表;
右侧: Lambda表达式中所需要执行的功能(Lambda体),对应的是对抽象方法的实现;(函数式接口(只能有一个抽象方法))
Lambda表达式的实质是 对接口的实现;
举例说明:用Lambda表达式创建线程
Thread thread = new Thread(() -> {
for (int i = 0; i < 5; i++) {
System.out.println("Thread:"+i);
}
});
thread.start();
三、Lambda表达式的具体实现方式
package com.study.lambda;
/**
* @Author gaojunlong
* @Date 2020/9/9
* @Time 9:21
* @Version 1.0
*/
//无参数,无返回值
@FunctionalInterface
public interface NoReturnNoParam {
void myNoReturnNoParam();
}
//无参数,有返回值
@FunctionalInterface
interface ReturnNoParam{
int myReturnNoParam();
}
//一个参数,无返回值
@FunctionalInterface
interface NoReturnOneParam{
void myNoReturnOneParam(int a);
}
//一个参数,有返回值
@FunctionalInterface
interface ReturnOneParam{
int myReturnOneParam(int a);
}
//多个参数,无返回值
@FunctionalInterface
interface NoReturnMoreParam{
void myNoReturnMoreParam(int a, int b);
}
//多个参数,有返回值
@FunctionalInterface
interface ReturnMoreParam{
int myReturnMoreParam(int a, int b);
}
package com.study.lambda;
/**
* @Author gaojunlong
* @Date 2020/9/9
* @Time 9:45
* @Version 1.0
*/
public class MyTest01 {
public static void main(String[] args) {
//无参数,无返回值
NoReturnNoParam noReturnNoParam = () -> {
System.out.println("NoReturnNoParam");
};
noReturnNoParam.myNoReturnNoParam();
System.out.println();
//无参数,有返回值
ReturnNoParam returnNoParam = () -> {
System.out.println("ReturnNoParam");
return 1;
};
int result = returnNoParam.myReturnNoParam();
System.out.println("return "+result);
System.out.println();
//一个参数无返回值
NoReturnOneParam noReturnOneParam = (int a) -> {
System.out.println("NoReturnOneParam: "+a);
};
noReturnOneParam.myNoReturnOneParam(8);
System.out.println();
//一个参数有返回值
ReturnOneParam returnOneParam = (int b) -> {
System.out.println("ReturnOneParam: "+b);
return 1;
};
int result1 = returnOneParam.myReturnOneParam(10);
System.out.println("result1: "+result1);
System.out.println();
//多个参数无返回值
NoReturnMoreParam noReturnMoreParam = (int x, int y) -> {
System.out.println("NoReturnMoreParam: {" + x +": " + y + "}" );
};
noReturnMoreParam.myNoReturnMoreParam(8,10);
System.out.println();
//多个参数有返回值
ReturnMoreParam returnMoreParam = (int num1, int num2) -> {
System.out.println("NoReturnMoreParam: {" + num1 +": " + num2 + "}" );
return num1 + num2;
};
int result2 = returnMoreParam.myReturnMoreParam(8,10);
System.out.println("result2:"+result2);
}
}
简化一:只有一个参数类型时,可以去掉参数类型(int、String等类型)
简化二:只有一个变量参数时,可以去掉括号()
简化三:只有一行代码时,可以去掉花括号{}
四、Lamdba表达式和其他表达式的区别
//1、定义一个函数式接口,并在接口中定义一个无参无返回的接口
@FunctionalInterface
interface ILike{
void lambda();
}
//2、实现类
class Like implements ILike{
@Override
public void lambda() {
System.out.println("I like lambda1");
}
}
//在主函数中实例化实现类,并调用该类的实现方法
public static void main(String[] args) {
ILike like = new Like();
like.lambda();
}
//3、静态内部类
static class Like2 implements ILike{
@Override
public void lambda() {
System.out.println("I like lambda2");
}
}
//在主类中调用方法
public static void main(String[] args) {
ILike like = new Like2();
like.lambda();
}
public static void main(String [] args){
//4、局部内部类
class Like3 implements ILike{
@Override
public void lambda() {
System.out.println("I like lambda3");
}
}
like = new Like3();
like.lambda();
}
public static void main(String[] args) {
//5、匿名内部类
like = new ILike() {
@Override
public void lambda() {
System.out.println("I like lambda4");
}
};
like.lambda();
}
public static void main(String[] args) {
//6、lambda表达式
like = () ->{
System.out.println("I l;ike lambda5");
};
like.lambda();
}
完整代码:
/**
* @Author gaojunlong
* @Date 2020/8/27
* @Time 11:18
* @Version 1.0
*/
public class TestLamdba {
//第二个方法、静态内部类
static class Like2 implements ILike{
@Override
public void lambda() {
System.out.println("I like lambda2");
}
}
public static void main(String[] args) {
ILike like = new Like();
like.lambda();
like = new Like2();
like.lambda();
//第三个方法、局部内部类
class Like3 implements ILike{
@Override
public void lambda() {
System.out.println("I like lambda3");
}
}
like = new Like3();
like.lambda();
//第四个方法、匿名内部类
like = new ILike() {
@Override
public void lambda() {
System.out.println("I like lambda4");
}
};
like.lambda();
//第五个方法,lambda表达式
like = () ->{
System.out.println("I l;ike lambda5");
};
like.lambda();
}
}
//定义一个函数式接口
@FunctionalInterface
interface ILike{
void lambda();
}
//第一种方法,实现类
class Like implements ILike{
@Override
public void lambda() {
System.out.println("I like lambda1");
}
}
运行结果:
I like lambda1
I like lambda2
I like lambda3
I like lambda4
I like lambda5