New Java 8 feature: Lambda helloworld

Posted by jola on Fri, 10 Dec 2021 17:56:18 +0100

★ this is Xiao Leng's blog
‡ see the column for high-quality technical articles
The official account of the individual, sharing some technical articles, and the pit encountered.
Current series: Java 8 new features series
Source code git warehouse Code Git warehouse address

New Java 8 features

Java 8 (also known as jdk 8) is a major version of Java language development. Java 8 was released by oracle in April 2014 and can be regarded as the most revolutionary version since java5. Java 8 has brought a lot of new features to the Java language, compiler, class library, development functions and JVM

Introduction to new features of Java 8

  • Faster
  • Less code (new syntax added: Lambada expression)
  • Powerful Stream API
  • It is convenient to improve the running speed of the program in parallel. For example, the fast sorting of the array with traversal length of 100k is 4.7 times faster
  • Minimize null pointer exceptions Optional
  • Nashom engine: allows JS applications to run on the Jvm

Lambda expression (emphasis)

Why use Lambda expressions

Lambda expression 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, we can write more concise and flexible code. As a more compact code style, the expression ability of using java language has been improved

Theory + practice to facilitate understanding

Lambda expression coding

Simply do a demo of hello world with Lambda

  • What this grammar mainly does is simplify our grammar
  • Most of the things that help us save are fixed to write
  • So as to simplify the grammar
/**
 * @author : <h2>Leng Huanyuan</h2>
 * @date : 2021/12/10
 * @context:<h4>Lambda Expression operation case < / H4 >
 */
public class LambdaTest {
    /**
     * <h2>Example 1 difference between parameterless traditional syntax and lambda expression</h2>
     */
    @Test
    public void Test1() {
        // Traditional create a thread print
        Runnable r1 = new Runnable() {

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

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


        // The method of using lambda expression
        Runnable r2 = () -> System.out.println("I love watching Tiananmen Square in Beijing");
        r2.run();
    }

    /**
     * @return :  * @return : null
     * @author : <h2>Leng Huanyuan</h2>
     * @date : 2021/12/10
     * @context: <h3>Traditional and new syntax of Lambda expressions with parameters</h3>
     * @params :  null
     */
    @Test
    public void Test2() {
        //Traditional syntax object correspondence method
        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("***********************");
        /*
         *  lambda Expression has parameter syntax
         *  Functional interface you must implement this interface, and you can use Lambda expressions as long as you first an interface
         *   @FunctionalInterface
         *   public interface Comparator<T> {
         *   int compare(T o1, T o2);
         *    }
         *  After using it, it becomes a very concise grammar. It replaces many known things in our province
         *  Comparator<Integer> com2 = (o1, o2) -> Integer.compare(o1, o2);
         * */
        Comparator<Integer> com2 = (o1, o2) -> Integer.compare(o1, o2);
        int compare2 = com2.compare(32, 21);
        System.out.println(compare2);
        System.out.println("***********************");

        //We can also simplify it more
        //Method reference
        Comparator<Integer> com3 = Integer::compare;
        int compare3 = com3.compare(20, 21);
        System.out.println(compare3);
        System.out.println("***********************");
    }

}

After a brief understanding of lambda expressions, let's take a look at the syntax changes of lambda expressions in different situations

/**
 * @author : <h2>Leng Huanyuan</h2>
 * @date : 2021/12/10
 * @context: <h4>
 * 1. Example sub (O1, O2) - > integer compare(o1,o2)
 * 2.Format:
 * -> Lambda Operator or arrow operator
 * ()The formal parameter list is equivalent to the parameter list of the abstract method in the interface, and the type can be omitted
 * ->The Lambda body on the right is actually the method body that rewrites the abstract object
 * 3.Lambda Use of expressions (there are six situations to introduce)
 * 4.Essence the essence of Lambda expressions: as instances of interfaces
 * </h4>
 */
public class LambdaTest1 {
    /**
     * @return :  * @return : null
     * @author : <h2>Leng Huanyuan</h2>
     * @date : 2021/12/10
     * @context: <h3>Syntax format 1: no parameter, no return value</h3>
     * @params :  null
     */
    @Test
    public void Test1() {
        // Traditional create a thread print
        Runnable r1 = new Runnable() {

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

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


        // The method of using lambda expression
        Runnable r2 = () -> System.out.println("I love watching Tiananmen Square in Beijing");
        r2.run();
    }

    /**
     * @return :  * @return : null
     * @author : <h2>Leng Huanyuan</h2>
     * @date : 2021/12/10
     * @context: <h3> Syntax expression 2 Lambda requires a parameter but does not return a value</h3>
     * @params :  null
     */
    @Test
    public void test2() {
        //Traditional differences
        Consumer<String> con = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        con.accept("The difference between a lie and an oath");
        System.out.println("**************************");
        //Using lambda
        Consumer<String> con2 = (String s) -> {
            System.out.println(s);
        };
        con2.accept("Give you a punch, give you another punch");
    }

    /**
     * @return :  * @return : null
     * @author : <h2>Leng Huanyuan</h2>
     * @date : 2021/12/10
     * @context: <h3>Syntax format 3: the data type can be omitted because it can be calculated by the compiler, which is called "type inference"“</h3>
     * @params :  null
     */
    @Test
    public void Test3() {
        //Using lambda can also simplify again
        Consumer<String> con2 = (String s) -> {
            System.out.println(s);
        };
        con2.accept("Give you a punch, give you another punch");

        //Type can also be omitted
        Consumer<String> con1 = (s) -> {
            System.out.println(s);
        };
        con1.accept("I dodge, dodge miss");
    }

    /**
     * @return :  * @return : null
     * @author : <h2>Leng Huanyuan</h2>
     * @date : 2021/12/10
     * @context: <h3> Examples of type inference</h3>
     * @params :  null
     */
    @Test
    public void Test4() {
        List<String> list = new ArrayList<>();
        int[] arrpub = {
                1, 2, 3
        };
    }

    /**
     * @return :  * @return : null
     * @author : <h2>Leng Huanyuan</h2>
     * @date : 2021/12/10
     * @context: <h3>Syntax format 4: when Lambda only needs one parameter, the parameter parentheses can also be omitted</h3>
     * @params :  null
     */
    @Test
    public void Test5() {
        //Type can also be omitted
        Consumer<String> con1 = (s) -> {
            System.out.println(s);
        };
        con1.accept("I dodge, dodge miss");
        //If the parameter has only one parenthesis, it can also be omitted
        Consumer<String> con2 = s -> {
            System.out.println(s);
        };
        con2.accept("I dodge, dodge miss");
    }

    /**
     * @return :  * @return : null
     * @author : <h2>Leng Huanyuan</h2>
     * @date : 2021/12/10
     * @context: <h3>Syntax format 5 Lambda requires more than two parameters, multiple execution statements, and can have return values</h3>
     * @params :  null
     */
    @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(02);
            }
        };
        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(02);
        };
        System.out.println(com2.compare(12, 21));
    }


    /**
     * @return :  * @return : null
     * @author : <h2>Leng Huanyuan</h2>
     * @date : 2021/12/11
     * @context: <h3>Syntax format 6: when there is only one statement in Lambda body, return and braces, if any, can be ignored</h3>
     * @params :  null
     */
    @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 night i miss you~~~~~~~~~");
        System.out.println("********************");
        Consumer<String> con2 = s -> System.out.println(s);
        con2.accept("I'll punch you");
    }
}

Today we'll learn here first,

summary

We know the new syntax added to java8,

  • It simplifies some repetitive and fixed code and enhances the simplicity of the code
  • Disadvantages, reduce the readability of the code

Topics: Java Back-end java8