Although the Java version has been updated to version 16, the Java 8 version still accounts for the majority on the market. In the following article, I will share with you a new feature in Java 8, lambda expression.
1. Introduction to lambda expression
lambda expression is one of the new features provided by Java 8, which can also be called closure; It supports simple functional programming in Java, that is, an anonymous function can be passed as a method parameter; The format is divided into three parts. The first part is the input parameter list, the second part is composed of - > fixed, and the third part is the method body;
public class LambdaTest { public static void main(String[] args) { // Creating threads using lambda expressions Thread thread = new Thread(() -> { System.out.println("thread running"); }); thread.start(); } }
//Operation results
thread running
2. Important features of lambda expressions
Optional parameter type declaration: there is no need to declare the type of the parameter, and the compiler can uniformly identify the parameter value;
public class LambdaTest { private Integer a; public LambdaTest(Integer a) { this.a = a; } public void print(LambdaInterface lambdaInterface) { lambdaInterface.print(this.a); } public static void main(String[] args) { LambdaTest lambdaTest = new LambdaTest(123); // Declare parameter type System.out.println("Declare parameter type"); lambdaTest.print((Integer a) -> { System.out.println("a: " + a); }); // Parameter types are not declared System.out.println("Parameter types are not declared"); lambdaTest.print((a) -> { System.out.println("a: " + a); }); } } interface LambdaInterface { void print(Integer a); }
//Operation results
Declare parameter type
a: 123
Parameter types are not declared
a: 123
Optional parameter parentheses: parentheses do not need to be defined for one parameter, but must be defined when there are no parameters or multiple parameters;
public class LambdaTest { private Integer a; public LambdaTest(Integer a) { this.a = a; } public void print(LambdaInterface lambdaInterface) { lambdaInterface.print(this.a); } public static void main(String[] args) { LambdaTest lambdaTest = new LambdaTest(123); // Define parameter parentheses System.out.println("Define parameter parentheses"); lambdaTest.print((a) -> { System.out.println("a: " + a); }); // Parameter parentheses may not be defined for a parameter System.out.println("Parameter parentheses may not be defined for a parameter"); lambdaTest.print(a -> { System.out.println("a: " + a); }); } } interface LambdaInterface { void print(Integer a); }
//Operation results
Define parameter parentheses
a: 123
Parameter parentheses may not be defined for a parameter
a: 123
Optional curly braces: if the method body has only one statement, curly braces are not required;
public class LambdaTest { private Integer a; public LambdaTest(Integer a) { this.a = a; } public void print(LambdaInterface lambdaInterface) { lambdaInterface.print(this.a); } public static void main(String[] args) { LambdaTest lambdaTest = new LambdaTest(123); // Use method body braces System.out.println("Use method body braces"); lambdaTest.print(a -> { System.out.println("a: " + a); }); // A statement may not use method body braces System.out.println("A statement may not use method body braces"); lambdaTest.print(a -> System.out.println("a: " + a)); } } interface LambdaInterface { void print(Integer a); }
//Operation results
Use method body braces
a: 123
A statement may not use method body braces
a: 123
Optional return keyword: if the method body has only one expression return value statement, it is not necessary to declare the return keyword, but it must be declared when braces exist;
public class LambdaTest { private Integer a; private Integer b; public LambdaTest(Integer a, Integer b) { this.a = a; this.b = b; } public Integer sum(LambdaInterface lambdaInterface) { return lambdaInterface.calculate(this.a, this.b); } public static void main(String[] args) { LambdaTest lambdaTest = new LambdaTest(123, 456); // Declare return keyword System.out.println("Declare return keyword"); Integer s1 = lambdaTest.sum((Integer a, Integer b) -> { return a + b; }); System.out.println(s1); // An expression return value statement may not declare a return keyword System.out.println("An expression return value statement may not declare a return keyword"); Integer s2 = lambdaTest.sum((a, b) -> a + b); System.out.println(s2); } } interface LambdaInterface { Integer calculate(Integer a, Integer b); }
//Operation results
Declare return keyword
579
An expression return value statement may not declare a return keyword
579
3. Restrictions of lambda expression on extraterritorial variables
lambda expressions have implicit final semantic restrictions on local variables outside the domain, but not on member variables;
public class LambdaTest { private Integer a; private Integer b; public LambdaTest(Integer a, Integer b) { this.a = a; this.b = b; } public Integer sum(LambdaInterface lambdaInterface) { return lambdaInterface.calculate(this.a, this.b); } public static void main(String[] args) { LambdaTest lambdaTest = new LambdaTest(123, 456); int c = 111; Integer s1 = lambdaTest.sum((a, b) -> { // If you modify a local variable outside the domain, a compilation error will occur c = 222; return a + b; }); System.out.println(s1); int d = 333; Integer s2 = lambdaTest.sum((a, b) -> { // Extraterritorial modification of extraterritorial local variables used inside lambda expressions will also lead to compilation errors return a + b + d; }); d = 444; System.out.println(s2); } } interface LambdaInterface { Integer calculate(Integer a, Integer b); }
public class LambdaTest { private Integer a; private Integer b; private Integer c; public LambdaTest(Integer a, Integer b, Integer c) { this.a = a; this.b = b; this.c = c; } public Integer sum(LambdaInterface lambdaInterface) { return lambdaInterface.calculate(this.a, this.b); } public static void main(String[] args) { LambdaTest lambdaTest = new LambdaTest(123, 456, 789); Integer s1 = lambdaTest.sum((a, b) -> { // No compilation errors occurred lambdaTest.c = 999; return a + b + lambdaTest.c; }); System.out.println(s1); } } interface LambdaInterface { Integer calculate(Integer a, Integer b); }
//Operation results
1578
4. Advantages and disadvantages of lambda expression
advantage:
1. Make the code more concise;
2. Reduce the creation of anonymous internal classes and save resources;
Disadvantages:
1. Poor maintainability, and must be familiar with the parameter list of abstract methods;
2. Poor readability, must have a certain depth of lambda expression;
5. Usage scenario of lambda expression
When declaring a method, if the formal parameter list of the method contains one or more functional interfaces, you can use lambda expressions; For example:
Create a thread by implementing the Runnable interface
Creating FutureTask using Callable interface
Four functional interfaces are used: Consumer interface, Supplier interface, Predicate interface and Function interface
6. Implementation principle of lambda expression
Lambda expression is a specific syntax that allows the compiler to compile java files into a corresponding static method for each lambda expression, which can also prove that lambda expression is not a syntax sugar;
// Yes, lambdatest LambdaTest.java compiled If you use javap -p to view the class file, you will get the following results javap -p LambdaTest.class Compiled from "LambdaTest.java" public class cn.jackiegu.java8.study.lambda.LambdaTest { private java.lang.Integer a; private java.lang.Integer b; private java.lang.Integer c; public cn.jackiegu.java8.study.lambda.LambdaTest(java.lang.Integer, java.lang.Integer, java.lang.Integer); public java.lang.Integer sum(cn.jackiegu.java8.study.lambda.LambdaInterface); public static void main(java.lang.String[]); private static java.lang.Integer lambda$main$0(cn.jackiegu.java8.study.lambda.LambdaTest, java.lang.Integer, java.lang.Integer); }