For basic grammar lambda expressions, one is to understand the basic grammar lambda expressions, and the other is the need to understand the function interface
A, lambda expressions describing basic grammar
java8 introduces new operator ->, can be called lambda operator or the arrow operator, the operator will be split into two parts lambda expression.
Left: parameter list
Right: code expressions to be executed, that is lambda body
lambda overall format are the following:
The first: no parameters and returns no value
The second: There are parameters, no return value
Variant 1: the type argument may be omitted
Variant 2: There is a parameter, the parameter may be omitted parentheses
Third: There parameters, return value
Variants: lambda statement execution body is only one, can be omitted and return lambda body braces
Two, lambda expressions sample
1, an expression, no parameters and returns no value
/** * lambda表达式一 */ public void test1(){ //jdk1.7之前 Runnable runnable = new Runnable() { @Override public void run() { System.out.print("hello runnable"); } }; runnable.run(); System.out.println("============================="); //使用lambda表达式 Runnable runnable1 = () -> System.out.println("hello lambda"); runnable1.run(); }
2, two expressions: There parameters and returns no value
public void test2(){ Comparator<Integer> comparator = (Integer x, Integer y) -> Integer.compare(x,y); }
Variant 1: the type of parameter may be omitted (this is because jvm compiler can be inferred from the context of the type at compile time, to infer the type of out argument; other here and do not have to be omitted are omitted, and some parameters can not be omitted, there parameter is not omitted)
public void test3(){ Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y); }
Variant 2: There is a parameter, the parameter may be omitted parentheses
public void test4(){ Consumer<String> consumer = x -> System.out.println(x); consumer.accept("hello"); }
Third: There parameters, return value
public void test5(){ Comparator<Integer> comparator = (Integer x, Integer y) -> { log.info("hello test5"); return Integer.compare(x,y); }; }
Variants: lambda body to perform only one statement, you can return and lambda body braces omitted (in fact, this has already been demonstrated, one kind of the second variant)
public void test3(){ Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y); }
to sum up:
In case of a left and right parentheses Province
Left of type inference Province
Third, the interface function
lambda expression needs to support functional interface, an interface function interface that is only one implementation. Annotations can be used to test @FunctionalInterface
Fourth, the sample interface function (java8 provides four core interface functions, follow-up will be described)
Create a function interface
package com.example.jdk8demo.lambda; @FunctionalInterface public interface MyInterface { Integer getInteger(Integer i); }
New function interface method calls
public Integer getInteger(Integer i, MyInterface mi){ return mi.getInteger(i); }
The final step is to use lambda expressions specific implementation logic
public void test6 () { // obtain 10 square log.info ( " ============ " + getInteger ( 10 , (X) -> X * X)); }