Lambda expression
Lambda expression overview
Lambda expressions, also known as closures, are the most important new features that promote the release of Java 8 .
Lambda allows a function to be used as a method parameter (a function is passed into the method as a parameter) .
Using Lambda expressions can make the code more concise and compact.
Lamda expressions are also called functional programming .
For example, y=kx+1;f(x)=kx+1; This is a function in mathematics. Given an x, you can get a value of y.
Lambda expressions are interface-oriented and interface-oriented programming. To function-oriented programming, in accordance with the principle of interface-oriented, it is necessary to define a functional interface .
Lambda expression syntax
Use anonymous inner classes to implement an interface
(parameters) -> expression
或
(parameters) ->{
statements; }
Lambda expression case
interface Fooi{
public void sayHello();
}
public class LambdaTest {
public static void main(String[] args) {
//接口不能new,使用匿名内部类
Fooi foo = new Fooi(){
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("hello 2004");
}
};
foo.sayHello();
}
}
The program here is originally only one line, but so much has been written. Reduce the efficiency of program development.
How can it be optimized?
Lambda expression is to solve the phenomenon of anonymous internal class code redundancy.
We can see that this interface has only one method, and when we want to implement this interface, there is only one method implemented. That is, you can know which method it is without specifying it.
Lambda expression formula: copy parentheses, write down right arrow, landing braces
interface Fooi{
public void sayHello();
}
public class LambdaTest {
public static void main(String[] args) {
Fooi foo=() -> {
System.out.println("hello 1205");};
foo.sayHello();
}
}
The code behind is only two lines. Significantly improved development efficiency.
If there are multiple methods now, can they still be used?
interface Fooi{
public void sayHello();
public int add(int x,int y);
}
public class LambdaTest {
public static void main(String[] args) {
Fooi foo=()->{
System.out.println("hello 1205");};
foo.sayHello();
}
}
At this time, the program reports an error:
It can be seen that only one method can be implemented in a functional interface to use Lambda expressions.
If there are parameters with return values, can they be used?
interface Fooi{
public int add(int x,int y);
}
public class LambdaTest {
public static void main(String[] args) {
Fooi foo=(int x,int y)->{
return x+y;
};
System.out.println(foo.add(3,5));
}
}
It can be seen that the method has parameters and return values can also use Lambda expressions
How to ensure that the interface is a functional interface?
Use @FunctionalInterface annotation
to add two methods to the interface, and an error will be reported. as the picture shows:
@FunctionalInterface;
interface Fooi{
public int add(int x,int y);
public void sale();
}
An error will be reported at this time.
If there is one and only one unimplemented method in an interface, but this annotation is not added, java will implicitly add this annotation to the interface.
If there are two methods in an interface and the FunctionalInterface annotation is defined, then Will report exception
Lambda expression summary
Lambda expression formula: copy the parentheses, write down the right arrow,
and use the @FunctionalInterface annotations