New features of java8: first, Lambda expression
1
Lambda expressions, also known as closures, are the most important new feature that drives the release of Java 8.
Lambda allows functions to be used as parameters of a method (functions are passed into the method as parameters).
Using Lambda expressions can make the code more concise and compact.
grammar
The syntax format of lambda expression is as follows:
(parameters) -> expression
or
(parameters) ->{ statements; }
The following are important features of lambda expressions:
Optional type declaration: there is no need to declare parameter types, and the compiler can uniformly identify parameter values.
Optional parameter parentheses: one parameter does not need to define parentheses, but multiple parameters need to define parentheses.
Optional curly braces: if the body contains a statement, curly braces are not required.
Optional return keyword: if the body has only one expression return value, the compiler will automatically return the value. Braces need to specify that the expression returns a value.
Lambda expression instance
Simple example of Lambda expression: (similar to the arrow function syntax of javascript, there is no parameter (), one parameter can be omitted (), and multiple parameters need ())
//1. No parameter is required. The return value is 5
() -> 5
//2. Receive a parameter (numeric type) and return twice its value
x -> 2 * x
//3. Accept 2 parameters (numbers) and return their difference
(x, y) -> x – y
//4. Receive two int integers and return their sum
(int x, int y) -> x + y
//5. Accept a string object and print it on the console without returning any value (it looks like returning void)
(String s) -> System.out.print(s)
public class Lam { public static void main(String[] args) { //Type declaration (Integer::sum, static method reference) // public static int sum(int a, int b) { // return a + b; // } // XiaoXuMath addition= Integer::sum; XiaoXuMath addition= (int a,int b)->a+b; //No type declaration XiaoXuMath subtraction=(a,b)->a-b; //Return statement in braces XiaoXuMath mul=(int a,int b)->{return a*b;}; //No braces and return statements XiaoXuMath div=(int a,int b)->a/b; System.out.println(addition.oper(12,54)); Lam h=new Lam(); System.out.println("12+54:"+h.getValue(12,54,addition)); System.out.println("7-9:"+h.getValue(7,9,subtraction)); System.out.println("5*4:"+h.getValue(5,4,mul)); System.out.println("28/7:"+h.getValue(28,7,div)); } interface XiaoXuMath{ int oper(int a,int b); } private int getValue(int x,int y,XiaoXuMath m){ return m.oper(x,y); } }
66 12+54:66 7-9:-2 5*4:20 28/7:4
You should pay attention to the following two points when using Lambda expressions:
Lambda expression is mainly used to define the method type interface executed in the line, for example, a simple method interface. In the above example, we use various types of lambda expressions to define the methods of the XiaoXuMath interface.
Lambda expressions avoid the trouble of using anonymous methods and give Java simple but powerful functional programming capabilities.
Variable scope
Lambda expressions can only refer to outer local variables marked final, which means that local variables defined outside the domain cannot be modified inside lambda, otherwise compilation errors will occur.
public class UseLam { final static String salutation="Hello?"; static String test="I am test"; public static void main(String[] args) { Greet g=msg -> System.out.println(salutation+msg); g.sayMessage("xiaoxu!"); WantToTest w1=e->e+"nihao"; System.out.println(w1.test1(test)); System.out.println(test); } interface Greet{ void sayMessage(String msg); } interface WantToTest{ String test1(String x1); // void test2(String x2); } }
Hello?xiaoxu! I am testnihao I am test
You can also access the local variables of the outer layer directly in the lambda expression:
public class UseLam{ public static void main(String[] args) { int num=1; Converter<Integer,String> c=i -> System.out.println("Hello:"+String.valueOf(i+num)); c.convert(45); System.out.println(c); System.out.println(c.getClass()); } @FunctionalInterface public interface Converter<T1,T2>{ void convert(int i); } }
Hello: 46 com.xiaoxu.boot.learn.UseLam$$Lambda$1/1198108795@6f539caf class com.xiaoxu.boot.learn.UseLam$$Lambda$1/1198108795
The local variable of lambda expression can not be declared as final, but it must not be modified by the following code (that is, it has implicit final semantics)
It is not allowed to declare a parameter or local variable with the same name as a local variable in a Lambda expression.
public static void main(String[] args) { String x=""; Comparator<String> c=(f,s)->Integer.compare(f.length(),s.length()); String a="1"; String b="78"; System.out.println(c.compare(a, b)); }
-1
Comparator is a functional interface, so you can instantiate assignment lambda, method reference and construction method reference
However, after successfully modifying a parameter with the same name as a local variable, an error will be reported: