/ * * A, basic grammar Lambda expressions: Java8 introduces a new operator "->" is called the arrow operator operator or operator Lambda * arrow operator Lambda expressions split into two parts : * * left: parameter list Lambda expressions * right: Lambda expressions need to perform the function, i.e., body Lambda * * a syntax: no parameters and returns no value * () -> System.out. println ( "! the Hello the Lambda"); * * syntax II: there is a parameter and no return value * (the X-) -> System.out.println (the X-) * * syntax three: If only one parameter, parentheses can be omitted * X -> System.out.println (X) * * four syntax: there are two or more parameters, return values, and a plurality of body Lambda statement * Comparator <Integer> com = ( x , Y) -> { * System.out.println ( "interface function"); * return Integer.compare (X, Y); } *; * * Syntax five: Lambda body if only one statement, return and curly braces can be omitted * Comparator <Integer> = COM (the X-, the y-) -> Integer.compare (the X-, the y-); * * syntax six : the data type of the parameter list of Lambda expression can be omitted, because the JVM compiler infer the context, data type, i.e. "type inference" * (X Integer, Integer Y) -> Integer.compare (X, Y) ; * * the Union: a case of left and right parentheses Province * Second line: the left infer the type Province * Streamer: the province can save * * two, Lambda expressions need to support "function interface" of * functional Interface: the interface is only one Interface abstract method, called a function interface. Annotations may be used @FunctionalInterface modified * function may check whether the interface * / public class TestLambda2 { @Test public void test1 () { int NUM = 0; // before jdk 1.7, must be the final ); } @Test public void test3(){ Comparator<Integer> com = (x, y) -> { System.out.println("函数式接口"); return Integer.compare(x, y); }; } @Test public void test4(){ Comparator<Integer> com = (x, y) -> Integer.compare(x, y); } @Test public void test5(){ // String[] strs; // strs = {"aaa", "bbb", "ccc"}; List <String> list = new new the ArrayList <> (); Show ( new new the HashMap <> ()); } public void Show (the Map <String, Integer> Map) { } // requirements: calculates for a number @Test public void test6 () { NUM Integer = Operation (100, (X) -> X * X); System.out.println (NUM); System.out.println (Operation ( 200 is, (Y) -> 200 is + Y )); } public Integer Operation (Integer NUM, MyFun MF) { return mf.getValue (NUM); } }