lambda语法lambda是JAVA8添加的新特性,他的本质是一个匿名函数。
在Java8中,当一个接口中的抽象方法被"default"修饰时,改方法在接口的实现类中可以不被实现。当一个接口如果想使用lambda表达式去实现时,改接口必须满足有且仅有一个方法要被实现(即只有一个没有被"default"修饰的方法), 这一类接口通常是使用"@FunctionalInterface"注解进行修饰,他们是函数式接口。
1.lambda的核心有3部分:参数列表、lambda运算符(->)和方法体
参数列表 :用来描述参数列表,通常使用()将参数包裹起来;
方法体:用来描述方法的具体逻辑,通常使用{}来将逻辑代码包裹起来;
lambda运算符:用来连接参数列表和方法体。
@FunctionalInterface
public interface LambdaSingleReturnMutipleParmeterService {
int test(int a,int b);
}
public class TestLambda {
public static void main(String[] args) {
LambdaSingleReturnMutipleParmeterService lambdaSingleReturnMutipleParmeterService = (int a, int b) ->{
return a + b;
};
int result = lambdaSingleReturnMutipleParmeterService.test(2, 4);
System.out.println("result:"+result);
}
}
2.lambda的参数列表中的参数类型可以省略,但是当有多个参数时,要么这些参数类型都同时省略,要么都不省略;因为lambda可以根据要实现接口或者方法来自动识别参数类型;
@FunctionalInterface
public interface LambdaSingleReturnMutipleParmeterService {
int test(int a,int b);
}
public class TestLambda {
public static void main(String[] args) {
LambdaSingleReturnMutipleParmeterService lambdaSingleReturnMutipleParmeterService = (a, b) ->{
return a + b;
};
int result = lambdaSingleReturnMutipleParmeterService.test(2, 4);
System.out.println("result:"+result);
}
}
4.当lambda的参数列表中的参数个数为0时,用来包裹参数的()不可以省略;
@FunctionalInterface
public interface LambdaNoneReturnNoneParmeterService {
void test();
}
public class TestLambda {
public static void main(String[] args) {
LambdaNoneReturnNoneParmeterService lambdaNoneReturnNoneParmeterService= ()->{ System.out.println("无参数无返回"); };
lambdaNoneReturnNoneParmeterService.test();
}
}
3.当lambda的参数列表中的参数个数为1时,用来包裹参数的()可以省略;
@FunctionalInterface
public interface LambdaSingleReturnSingleParmeterService {
int test(int n);
}
public class TestLambda {
public static void main(String[] args) {
LambdaSingleReturnSingleParmeterService lambdaSingleReturnSingleParmeterService = a ->{
return 2 * a;
};
int result = lambdaSingleReturnSingleParmeterService.test(10);
System.out.println("result:"+result);
}
}
方法的引用
方法引用的实质其实是将一个lambda表达式的实现指向一个已经实现的方法;用这个方法来替代lambda表达式的实现。
根据方法所属者的不同,可以将方法引用分为静态方法引用和普通方法引用2种:静态方法引用:方法的所属者为类,即方法被"static"修饰;
普通方法引用:方法的所属者为对象,即方法没有被"static"修饰;
@FunctionalInterface
public interface LambdaSingleReturnSingleParmeterService {
int test(int n);
}
静态方法引用
public class TestLambda {
public static void main(String[] args) {
LambdaSingleReturnSingleParmeterService lambdaSingleReturnSingleParmeterService = TestLambda::change;
int result = lambdaSingleReturnSingleParmeterService.test(10);
System.out.println("result:"+result);
}
public static int change(int a){
return a*2;
}
}
普通方法引用
public class TestLambda {
public static void main(String[] args) {
TestLambda testLambda = new TestLambda();
testLambda.TestChange(10);
}
public int change(int a){
return a*2;
}
public void TestChange(int a){
TestLambda testLambda = new TestLambda();
//
LambdaSingleReturnSingleParmeterService lambdaSingleReturnSingleParmeterService = testLambda::change;
int result = lambdaSingleReturnSingleParmeterService.test(a);
System.out.println("result:"+result);
}
}
构造方法引用
public class Person {
private String name;
private int age;
public Person() {
System.out.println("Person的无参构造方法执行");
}
public Person(String name, int age) {
this.name = name;
this.age = age;
System.out.println("Person的有参构造方法执行");
}
}
public class TestLambda {
public static void main(String[] args) {
// FirstPersonCrete firstPerson = ()-> new Person();
// firstPerson.crete();
// SecondPersonCrete secondPerson = (name, age)-> new Person();
// secondPerson.crete("张三",10);
FirstPersonCrete firstPerson = Person::new;
firstPerson.crete();
SecondPersonCrete secondPerson = Person::new;
secondPerson.crete("张三",10);
}
}
interface FirstPersonCrete{
Person crete();
}
interface SecondPersonCrete{
Person crete(String name, Integer age);
}



