Basic syntax of Lambda expressions

1. Basic Grammar

Java8 introduces a new operator "->" arrow operator, the arrow operator divides the Lambda expression into two parts

On the left: the parameter list of the Lambda expression, corresponding to the parameter list of the abstract method

Right: Functions to be performed, corresponding to functions to be implemented by abstract methods

2. Tips

Save a bracket between left and right, infer type on the left,

3. Grammatical format

Syntax format 1: no parameters, no return value

() ->System.out.println("Hello");

   @Test
     public  void test1 () {
         int num = 2; // Before jdk 1.7, it must be final before it can be called by the same level 
        Runnable r = new Runnable () { 
            @Override 
            public  void run () { 
                System.out.println ( "Hello" + num); 
            } 
        }; 
        r.run (); 

        System.out.println ( "---------------------" );
         // Use Lambda expression 
       Runnable r2 = ()-> System.out.println ("Hello Lambda" ); 
        r2.run (); 
    }

 

Syntax format 2: There is one parameter, no return value

    @Test
     public  void test2 () { 
        Consumer <String> con = (x)-> System.out.println (x); 
        con.accept ( "There is a parameter" ); 
    }

 

Syntax format 3: If there is only one parameter, the parentheses can be omitted without writing

    @Test
     public  void test2 () { 
        Consumer <String> con = x-> System.out.println (x); 
        con.accept ( "There is a parameter" ); 
    }

 

Syntax format four: There are two parameters, there is a return value, and there are multiple statements in the Lambda body

  @Test
     public  void test4 () { 
        Comparator <Integer> com = (x, y)-> { 
            System.out.println ("Functional Interface"); 
            return Integer.compare (x, y); 
        }; 
    }

 

Syntax format 5: There are two parameters, there is a return value, and there is only one statement in the Lambda body, neither return nor {} can be written

    @Test
    public void test5() {
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);

    }

 

Syntax format 6: The data type in the parameter list of the Lambda expression can be omitted or not written,

                      Because the JVM compiler infers from the context, the data type, ie "type inference"

                      (Integer x, Integer y) -> Intrger.compare (x, y);

4. Lambda expression needs "functional interface" support

Functional interface: There is only one abstract method interface in the interface, which can be decorated with @FunctionalInterface. You can check whether it is a functional interface

// Interface, only one parameter, interface with return value 
package
Clock; @FunctionalInterface public interface MyFun { public Integer getValue (Integer x); }
 // Requirement: Perform an operation on a number 
    @Test
     public  void test7 () { 
        Integer num = operation (100, x-> x * x); 
        System.out.println (num); 
        System.out.println (operation ( 200 , y-> y + 200 )); 

    } 

    public Integer operation (Integer num, MyFun my) {
         return my.getValue (num); 
    }

 

Guess you like

Origin www.cnblogs.com/wangxue1314/p/12756186.html