The above briefly introduces some new features and advantages of java8, and also formulates a learning direction for learning the new features of java8 this time. This article will start learning from the more important Lambda expressions in the new features of java8.
1. Why use Lambda expressions
Lambda is an anonymous function, we can understand baLambda expression as a piece of code that can be passed (pass the code like data). Can write more concise and flexible code. As a more compact code style, the language expression ability of java is improved. Lambda expressions require the support of functional interfaces. An interface with only one abstract method in an interface is called a functional interface. It can be decorated with @FunctionalInterface to check whether it is a functional interface.
2. Basic syntax of Lambda expressions
A new operator has been added in java8, the "->" arrow expression, also known as the Lambda operator. The left side of the Lambda expression is the parameter list of the Lambda expression, and the right side is the operation steps and logic, also called the Lambda body. Lambda expressions have several syntax formats, and we will learn them one by one below.
1. No parameters, no return value (() -> interface function).
1 /** 2 * 3 * Syntax 1: no parameters, no return value (() -> executed code) 4 */ 5 @Test 6 public void test1() { 7 // java8 implements Runnable method 8 Runnable run = () -> System.out.println("Hello Lambda" ); 9 run.run(); 10 11 // 12 before java8 Runnable run1 = new Runnable() { 13 14 @Override 15 public void run() { 16 System.out.println("Hello World"); 17 } 18 }; 19 run1.run(); 20 }
It should be noted that before java1.7, when using a local variable of the same level in an anonymous inner class, the local variable must be of final type, but after java8, local variables are not modified by final, and final can be omitted. , but its type is still final by default.
2. One parameter, no return value
1 /** 2 * Syntax 2: 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 parameter, the parentheses can be omitted.
1 /** 2 * Syntax 2: 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 parameters, multiple statements, and return values.
1 /** 2 * Syntax 3: Multiple parameters, multiple statements, and return values. 3 */ 4 @Test 5 public void test3() { 6 Comparator<Integer> com = (x, y) -> { 7 System.out.println(x); 8 System.out.println(y); 9 return 0 ; 10 }; 11 System.out.println(com.compare(1, 3 )); 12 }
4. Multiple parameters, one statement, return value
1 /** 2 * Syntax 4: Multiple parameters, one statement, and return value 3 */ 4 @Test 5 public void test4() { 6 // When there is only one statement and return value, both curly brackets and return can be omitted 7 Comparator<Integer> com = (x, y) -> 0 ; 8 9 System.out.println(com.compare(1, 3 )); 10 }
Note: When there is only one statement and return value, both curly brackets and return can be omitted.
5. The parameter list of Lambda expressions can be omitted, because the JVM compiler can infer the type through the context, that is, "type inference"
3. Lambda expressions need the support of "functional interfaces"
Functional interface: When there is only one abstract method in the interface, then the interface is called a functional interface. A functional interface can be decorated with @FunctionalInterface, and the jvm will automatically check whether the interface is a functional interface.
1 @FunctionalInterface 2 public interface myLambdaModle<T> { 3 4 public int test(T t1, T t2); 5 6 }
Requirement: operate on two numbers
1 @Test 2 public void test5() { 3 int sum1 = myTest(3, 6, (x, y) -> x*y); 4 int sum2 = myTest(3, 6, (x, y) -> x-y); 5 int sum3 = myTest(3, 6, (x, y) -> x+y); 6 int sum4 = myTest(3, 6, (x, y) -> x/y); 7 } 8 9 10 public int myTest(int o1, int o2, myLambdaModle<Integer> m) { 11 return m.test(o1, o2); 12 }
In this way, we can use an interface to perform operations on numbers, and we can define the specific operations ourselves.
It can be seen from this article that before JDK1.7, if you define an abstract method, a method, you can only implement one operation, but now, the implementation can define the code by itself, which simplifies a lot. The following essays will continue to learn the four built-in core functional interfaces of java8.