Lambda expression in Java 8
- Lambda expression is a new feature of Java 8
- Lambda expressions are primarily used to support the use of code blocks as method parameters, allowing for the creation of instances of functional interfaces using cleaner code, a simplification of anonymous inner classes that can partially replace the role of anonymous inner classes.
- Functional interface: An interface with only one abstract method.
- Lambda expressions, also known as anonymous functions (anonymous function), represent a class of functions or subroutines that do not need to define identifiers (function names), which can be considered a syntactic sugar .
LAMBDA expression Syntax format
A lambda expression is made up of three parts in Java:
- Formal parameter lists: parameter lists allow omitting parameter types, and parentheses outside the formal parameter list can be omitted if the parameter list has only one argument
- Arrows->: arrows cannot be omitted
- Code block: If the code block has only one statement, the curly braces of the code block are allowed; even more, there is only one return statement, which can omit the return keyword.
Interfaceeatable{void Taste();}Interfaceflyable{void Fly(String weathrer);}Interfaceaddable{int Add(intAintb);} Public classlambdatest { Public void Eat(eatable e) {e.Taste(); System. out.println(e); } Public void Fly(flyable f) {F.Fly("Good weather"); System. out.println(f); } Public void Add(Addable a) {System. out.println(A.Add(5,3)); } Public Static void Main(string[] args) {Lambdatest test =New lambdatest(); Test.Eat(()->system. out.println("Apple")); Test.Fly(weater->{System. out.println("hahaha"); System. out.println("Can Fly"); });//Omit formal parameter type omit curly bracesTest.Add(A, b)->a+b); }}
Function-Type interface
- A functional interface is an interface that contains only an abstract method, (there can be other default methods, or class methods) Java 8 has a large number of functional interfaces, such as the most common: Runnable,actionlistener, @functioninterface annotations are provided, which tells the compiler to perform more stringent checks.
Runnbale r =()->{ for(int i =0;i<100;i++){ System.out.println(i); // 注意分号
- The target type of the LAMDBA expression can only be a functional interface.
- Lambda expressions often apply scenarios:
- Assigning a lambda expression to a variable of a functional interface type
- Passed to a method as a parameter of the function interface type
- Using a functional interface to Force type conversions on lambda expressions
Object obj = (Runnable)()->{ System.out.println(...);};
Method references and constructor references
- Scenario: A code block has only one statement, and the statement is a calling method or constructor
- Purpose of application: to make the syntax of a lambda expression more concise
- Application Method:::
@FunctionalInterfaceinterface Converter{ convert(String from);}// 类方法Converter con = from->Integer.valueOf(from);Converter con1 = Integer::valueOf;// 对象方法Converter con2 = from->"hhhh".indexOf"hhhh"::indexOf;// 构造器类似// ::new
The relationship and difference between lambda expressions and anonymous inner classes
- Lambda expressions simplify the notation of some anonymous inner classes (functional interfaces), which are consistent with anonymous inner classes, access to effective final local variables, and external class member variables
- Anonymous inner classes are more widely used, can create instances for any interface, and can serve abstract and generic classes
- Note the code block of a lambda expression does not allow the default method defined in the calling interface
Lambda expression in Java 8