New features of Java 8 -- Lambda expression

New features of Java 8 -- Lambda expression


Before Java 8, we wanted a method to interact with the user, such as using local variables within the method. At this time, we can only use the interface as a parameter, so that the user can implement the interface or use the form of anonymous inner class to pass the local variables to the user through the interface method.

The essence of Lambda expression: as an example of functional interface

Use of Lambda expressions: (divided into 6 common situations)

1. The first usage

Syntax format 1: no parameter, no return value

@Test
    public void test1(){
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("I Love Beijing Tiananmen ");
            }
        };

        r1.run();

        System.out.println("***********************");
       //TODO: Lambda expression writing
        Runnable r2 = () -> {
            System.out.println("I love the Forbidden City");
        };

        r2.run();
    }

2. The second usage

//Syntax format 2: Lambda requires a parameter, but does not return a value.

 @Test

    public void test2(){
        Consumer<String> con = new Consumer<String>() {
            @Override
            public void accept(String s) {

                System.out.println(s);

            }
        };

        con.accept("What's the difference between a lie and a promise?");
        System.out.println("*******");
        
        //TODO: Lambda expression writing
        Consumer<String> con1 = (String s) -> {

            System.out.println(s);

        };

        con1.accept("One is to listen to people seriously, the other is to say people seriously");

 

}

3. The third usage

Syntax format 3: data types can be omitted because they can be inferred by the compiler, which is called "type inference"

 @Test

    public void test3(){

        Consumer<String> con1 = (String s) -> {

            System.out.println(s);

        };

        con1.accept("One is to listen to people seriously, the other is to say people seriously");

        System.out.println("*******");

        //TODO: Lambda expression writing
        Consumer<String> con2 = (s) -> {

            System.out.println(s);

        };

        con2.accept("One is to listen to people seriously, the other is to say people seriously");
}

4. Fourth use

Syntax format 4: if Lambda only needs one parameter, the bracket of the parameter can be omitted

    @Test

    public void test5(){

        Consumer<String> con1 = (s) -> {

            System.out.println(s);

        };

        con1.accept("One is to listen to people seriously, the other is to say people seriously");
        System.out.println("*******");

        Consumer<String> con2 = s -> {

            System.out.println(s);

        };

        con2.accept("One is to listen to people seriously, the other is to say people seriously");

}

5. Fifth use

Syntax format 5: Lambda requires two or more parameters, multiple execution statements, and return values

@Test
public void test6(){
    Comparator<Integer> com1 = new Comparator<Integer>() {

        @Override

        public int compare(Integer o1, Integer o2) {

            System.out.println(o1);

            System.out.println(o2);

            return o1.compareTo(o2);

        }

    };
    System.out.println(com1.compare(12,21));

 

    System.out.println("*********");

    Comparator<Integer> com2 = (o1,o2) -> {

        System.out.println(o1);

        System.out.println(o2);

        return o1.compareTo(o2);

    };

    System.out.println(com2.compare(12,6));

}

6. Sixth case

//Syntax format 6: when there is only one statement in Lambda body, return and braces can be omitted

    @Test

    public void test7(){

 

        Comparator<Integer> com1 = (o1,o2) -> {

            return o1.compareTo(o2);

        };

 

        System.out.println(com1.compare(12,6));

 

        System.out.println("*********");

 

        Comparator<Integer> com2 = (o1,o2) -> o1.compareTo(o2);

 

        System.out.println(com2.compare(12,21));

 

}
@Test

    public void test8(){

        Consumer<String> con1 = s -> {

            System.out.println(s);

        };

        con1.accept("One is to listen to people seriously, the other is to say people seriously");

 

        System.out.println("*********");

 

        Consumer<String> con2 = s -> System.out.println(s);

 

        con2.accept("One is to listen to people seriously, the other is to say people seriously");

 

    }
Published 3 original articles, praised 0 and visited 15
Private letter follow

Keywords: Lambda Java

Added by Tea_J on Wed, 11 Mar 2020 13:20:46 +0200