In Java8, Lambda (Lambda expression) was added. Before using it, let’s talk about why Lamdba was added and what programming benefits it has
I. Role of Lamdba
1. Before Java 8, we needed to write anonymous inner classes when we needed to pass some functionality to some method. By introducing lambda expressions, you can easily define arguments and methods in a class, making code more elegant by replacing most anonymous inner classes and function interfaces. Lambda provides a list of arguments and a function body that uses only those arguments, also known as closures.
Use Lambda expressions to know the specification
1. A Lambda must correspond to an interface, and the interface is required to implement only one method. If multiple methods are written, there will be more than one abstract method in the interface when writing Lambda expressions. You cannot encode using lambda.
2. To tell an application that an interface is functional, add the @functionallnterFace annotation to the interface.
3. Lambda expression format
1. The syntax is () -> {}, where () is the argument list, {} is the method body, -> is a Lambda expression, can be understood as an arrow, provides parameters for the method body {}.
Start by creating two interfaces
@FunctionalInterface
public interface LambdaNoreture2 {
void method(string a,string b);
}
@FunctionalInterface
public interface LambdaNoreture1 {
void method(string a);
}
Copy the code
Basic Lambda expression format (argument A, argument B) ->{method body}
public class Lambda { public static void main(String[] args) { LambdaNoreture2 lambdaNoreture2=(String a,String b)->{ System.out.println("Hello" + a + b +"!" ); }; lambdaNoreture2.method("Lam","bda"); } // Output HelloLambda!Copy the code
Lambda shorthand: If there is only one argument and method, omit the parentheses () and curly braces {}; If multiple or one parameter type is the same, you can omit the parameter type as follows.
public class Lambda { public static void main(String[] args) { LambdaNoreture1 lambdaNoreture1=a-> System.out.println("Hello" + a + b +"!" ); lambdaNoreture1.method("Lam","bda"); } // Output HelloLambda!Copy the code
4, because every time we need to use Lambda expressions to create a corresponding functional interface, it is very troublesome, so JDK8 intimate to give us some functional interface, most cases we do not need to create new interface, directly use can, which we mainly learn the next four most commonly used interface;
Function<T,R>, Fcuntion receives a T (generic) type parameter, returns a method of type R, and executes the content by calling the Apply method. The following example is an example of using Fcuntion
import java.util.function.Function; Public class LambdaFcuntion {public static void main(String[] args) {Function<Double,Double> S=r->r*r*3.14+67.84; System. The out. Println (S.a pply (12.0)); } // Output: 520.0Copy the code
Predicate <T,Boolean>, the Predicate interface is used as a filter for determining whether the class is mature. The Predicate interface has a parameter and a Boolean return value.
import java.util.function.Predicate; public class LambdaPredicate { public static void main(String[] args) { Predicate<Integer> predicate=age->age>=18; System.out.println(predicate.test(5)); System.out.println(predicate.test(20)); }} //test (5) Output: false //test (20) Output: trueCopy the code
Consume interface, Consum is also called Consume interface. The corresponding method type of this interface is to receive a parameter and return no value. The following is an example.
import java.util.function.Consumer; public class Consum { public static void main(String[] args) { Consumer<String> printString = s -> System.out.println(s); Printstring. accept(" Consumer succeeded, this is your Consumer"); }} // Output result: successful consumption, this is your ConsumerCopy the code
The Supplier interface, as opposed to Consume, has no arguments and only return values, so it is also called the give interface. You can use the get() method to get this return value. Here is an example.
import java.util.function.Supplier; public class Supplier{ public static void main(String[] args) { Supplier<String> Supplier = () ->"I get Supplier"; System.out.println(Supplier.get()); }} // I get SupplierCopy the code