Introduction to new features of Java 8 & Lambda expression

catalogue

1, Introduction to new features of Java 8

characteristic

2, Parallel stream and serial stream

3, Do you want to use a Lambda expression

3, Example of Lambda expression

1.Runnable

2.Comprator

(1) Use of Lambda expressions

1. Format

2. Syntax format 1: no parameter, no return value

3. Syntax format 2: lambda requires a parameter, but there is no return value

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

5. Syntax format 4: if lambda only needs one parameter, the parentheses of the parameter can be omitted

6. Syntax format 5: lambda two or more parameters, multiple execution statements, and can have return values

7. Syntax format 6: when there is only one statement in the lambda body, return and braces, if any, can be omitted

 8. summary

(2) The essence of lambda expression

1, Introduction to new features of Java 8

Java 8 (also known as jdk 1.8) is a major version of Java language development

Java8, released by oracle in March 2014, can be regarded as the most revolutionary version since Java5. Java 8 brings a lot of new features with Java language, compiler, class library, development tools and JVM

characteristic

  • Faster

  • Less code (new syntax added: Lambda expression)

  • Powerful Stream API

  • Easy parallel

  • Minimize null pointer exceptions: Optional

  • Nashorn engine, which allows JS applications to run on the JVM

2, Parallel stream and serial stream

A content is divided into multiple data blocks, and different threads are used to process the flow of each data block. Compared with serial streams, parallel streams can greatly improve the execution efficiency of programs

Java 8 optimizes parallelism. We can easily operate data in parallel

The Stream API can declaratively switch between parallel and sequential streams through parallel() and sequential()

3, Do you want to use a Lambda expression

Lambda is an anonymous function. We can understand lambda expression as a piece of code that can be passed (pass the code like data). Using it can

+Write more concise and flexible code. As a more compact code style, the expression ability of Java language has been improved

3, Example of Lambda expression

1.Runnable

    @Test
    public void test1(){
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("happy new year");
            }
        };
        r1.run();
        System.out.println("****************************");
        Runnable r2 = () -> System.out.println("2022 happy new year");

        r2.run();
    }

2.Comprator

    @Test
    public void test2(){
        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1,o2);
            }
        };
        int compare1 = com1.compare(12,21);
        System.out.println(compare1);
        System.out.println("***********************");
        //Writing method of Lambda expression
        Comparator<Integer> com2 = (o1,o2) -> Integer.compare(o1,o2);
        int compare2 = com2.compare(32,21);
        System.out.println(compare2);
        System.out.println("***********************");
        //Method reference
        Comparator<Integer> com3 = Integer :: compare;
        int compare3 = com2.compare(50,21);
        System.out.println(compare3);
    }

(1) Use of Lambda expressions

1. Format

(o1,o2) -> Integer.compare(o1,o2);

->: lambda operator (arrow operator)

->Left: lambda formal parameter list, which is actually the formal parameter list of abstract methods in the interface

->Right: the lambda body is actually the method body of the rewritten abstract method

2. Syntax format 1: no parameter, no return value

    @Test
    public void test1(){
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("happy new year");
            }
        };
        r1.run();
        System.out.println("****************************");
        Runnable r2 = () -> System.out.println("2022 happy new year");

        r2.run();
    }

3. Syntax format 2: lambda requires a parameter, but there is no return value

    @Test
    public void test2(){
        Consumer<String> con = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        con.accept("What do you like");
        System.out.println("**********************");
        Consumer<String> con1 = (String s) -> {
            System.out.println(s);
        };
        con1.accept("I like Xiao Zhu");
    }

4. Syntax format 3: the data type can be omitted because it 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("The full stop of winter is spring");
        System.out.println("*****************");
        Consumer<String> con2 = (s) -> {
            System.out.println(s);
        };
        con2.accept("The full stop of winter is spring~");
    }
    @Test
    public void test4(){
        ArrayList<String> list = new ArrayList<>();  //Type inference
        int[] arr = {1,2,3}; //Type inference
    }

5. Syntax format 4: if lambda only needs one parameter, the parentheses of the parameter can be omitted

    @Test
    public void test5(){
        Consumer<String> con1 = (s) -> {
            System.out.println(s);
        };
        con1.accept("The full stop of winter is spring");
        System.out.println("****************");
        Consumer<String> con2 = s -> {
            System.out.println(s);
        };
        con2.accept("The full stop of winter is spring~");
    }

6. Syntax format 5: lambda two or more parameters, multiple execution statements, and can have 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));
    }

7. Syntax format 6: when there is only one statement in the lambda body, return and braces, if any, 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("The full stop of winter is spring");
        System.out.println("**********************");
        Consumer<String> con2 = s -> System.out.println(s);
        con1.accept("The full stop of winter is spring");
    }

 8. summary

->Left: the parameter type of lambda parameter list can be omitted (type inference); If the lambda parameter list has only one parameter, a pair of parentheses can also be omitted

->Right: lambda body is wrapped with a pair of {}; If the lambda body has only one execution statement (possibly a return statement), you can omit this pair of {} and return keywords

(2) The essence of lambda expression

As an instance of an interface: an interface has only one abstract method

Keywords: Java Maven

Added by francisexpress on Fri, 28 Jan 2022 19:19:53 +0200