The above briefly introduces some of the new features of JAVA8, and the advantages, but also for this study java8 new features to develop a learning direction, the next few will be based on the new features of the above to start learning. In this paper, we begin to learn from the more important lambda expressions in java8 new features.
First, why use lambda expressions
Lambda is an anonymous function, and we can balambda the expression as a piece of code that can be passed (passing code like data). You can write more concise, more flexible code. As a more compact code style, Java's ability to express language is improved. Lambda expressions require the support of a functional interface, where only an abstract method interface is called a functional interface. You can use the @functionalinterface adornment to check if it is a functional interface.
Ii. basic syntax for lambda expressions
A new operator, the "--" arrow expression, also known as the lambda operator, is added to the java8. The lambda expression has a list of arguments to the left of the lambda expression, and the right side is the action step and the logic, also called the Lambda body. There are several syntax formats for lambda expressions, and here's how to learn them.
1. No parameter, no return value ((), interface function).
1 /**2 * 3 * Syntax One: no parameters, no return value (() code executed)4 */5 @Test6 Public voidtest1 () {7 //Java8 Implementing the Runnable method8Runnable Run = ()-System.out.println ("Hello Lambda");9 Run.run ();Ten One //Java8 used to be written. ARunnable run1 =NewRunnable () { - - @Override the Public voidrun () { -System.out.println ("Hello World"); - } - }; + Run1.run (); -}
One needs to be aware that Before java1.7, when a local variable of the same level was used in an anonymous inner class, the local variable must be of the final type, but after java8, the local variable is not final decorated, final can be omitted, but its type is the default final type.
2. One parameter, no return value
1 /** 2 * Syntax two: one parameter, no return value 3 */ 4 @Test 5 Public void test2 () {6 Consumer<string> t = (x)- System.out.println (x); 7 T.accept ("Hello Lambda"); 8 }
Note that if there is only one argument, the parentheses may not be written.
1 /** 2 * Syntax two: one parameter, no return value 3 */ 4 @Test 5 Public void test2 () {6 consumer<string> t = x -System.out.println (x); 7 T.accept ("Hello Lambda"); 8 }
3. Multiple arguments, multiple statements, and return values.
1 /**2 * Syntax Three: Multiple arguments, multiple statements, with return values. 3 */4 @Test5 Public voidtest3 () {6comparator<integer> com = (x, y) {7 System.out.println (x);8 System.out.println (y);9 return0;Ten }; OneSystem.out.println (Com.compare (1, 3)); A}
4. Multiple parameters, one statement, with return value
1 /**2 * Syntax Four: multiple parameters, one statement, with return value3 */4 @Test5 Public voidtest4 () {6 //Curly and return can be omitted when there is only one statement and return value7comparator<integer> com = (x, y)-0;8 9System.out.println (Com.compare (1, 3));Ten}
Note: curly braces and return can be omitted when there is only one statement and return value.
The argument list of the 5.LAMBDA expression can be used without writing because the JVM compiler can infer the type through the context, which is "type inference"
Third, lambda expression requires "functional interface" support
Functional interface: When there is only one abstract method in an interface, then this interface is called a functional interface. The functional interface can be decorated with @functionalinterface, and the JVM automatically checks to see if the interface is a functional interface.
1 @FunctionalInterface 2 Public interface mylambdamodle<t> {34public int Test (t T1, T T2); 5 6 }
Requirement: Two numbers to be calculated
1 @Test2 Public voidTest5 () {3 intSUM1 = MyTest (3, 6, (x, y), x*y);4 intsum2 = MyTest (3, 6, (x, y)-Xy);5 intSUM3 = MyTest (3, 6, (x, y), x+y);6 intSUM4 = MyTest (3, 6, (x, y), x/y);7 }8 9 Ten Public intMyTest (intO1,intO2, Mylambdamodle<integer>m) { One returnm.test (O1, O2); A}
In this way, we can use an interface to operate on the number of numbers, specifically what can be defined by themselves.
As can be seen from this article, before the original JDK1.7, if you define an abstract method, a method, you can only implement an operation, and now, the implementation can completely define their own code to simplify a lot. Later essays will continue to learn Java8 's four built-in core functional interfaces.
Java8 new feature--LAMBDA expression