Lambda expression to explain the contrast with the traditional interface function implementation

In some articles written before in this number, I use a lambda expression syntax, some readers have reported that the code can not read. I thought java 13 have been out, java 8 of the most important characteristics of the lambda expression we should have mastered, in fact, still there are a lot of programmers do not use java8, as well as the use of java8 will not use lambda expressions. So, this writing is still necessary, if you think my article helpful to you, look forward to your attention .

Lambda expressions are Java 8 most popular and commonly used features. It will introduce the concept of functional programming Java, the benefits of functional programming is that it can help us save a lot of code, very easy to use, can greatly improve our coding efficiency. In this article, we will describe what the lambda expression is, and converts the traditional java code written for the lambda expression written, we can understand by example lambda expression to what had been done to simplify legacy code.

First, the interface definition

First of all, we need to understand what the lambda expression in the expression? The answer is a lambda expression expression implementation of the interface functions, so we need to do some preparatory work. In the traditional development methods, we are not used will be passed to the function block. All of our behavior code definitions are encapsulated in vivo, and executed by the object reference, use the following code as the same:


public class LambdaDemo {
    //函数定义
    public void printSomething(String something) {
        System.out.println(something);
    }
    //通过创建对象调用函数
    public static void main(String[] args) {
        LambdaDemo demo = new LambdaDemo();
        String something = "I am learning Lambda";
        demo.printSomething(something);
    }
}

We should not feel strange way for the development of the above code, which is the realization of the classical OOP style. Let's do a modification of the above code, create a functional interface, and the interface to define abstract methods.


public class LambdaDemo {
    //抽象功能接口
    interface Printer {
        void print(String val);
    }
    //通过参数传递功能接口
    public void printSomething(String something, Printer printer) {
        printer.print(something);
    }
}

Second, the implementation of conventional interface function

In the above implementation, Printer interface is responsible printing behavior, the console can be printed, printing may be other actions. Methods printSomething no longer defined behavior, but performs Printer behavior defined, this design is more flexible. code show as below:


public static void main(String[] args) {
    LambdaDemo demo = new LambdaDemo();
    String something = "I am using a Functional interface";
    //实现Printer接口
    Printer printer = new Printer() {
        @Override
        public void print(String val) {
            //控制台打印
            System.out.println(val);
        }
    };
    demo.printSomething(something, printer);
}

So far we have yet to use lambda expressions. We only create a concrete realization Printer interface, and passes it to printSomething method.

Three, lambda expressions implementation

After the lambda expression on the concept paper to say, let's learn about the syntax of lambda expressions:

(param1,param2,param3 ...,paramN)-  > {   //代码块;  }
  • First, we know that lambda expression, the expression is a function of the interface
  • The left arrow is a comma-separated list of arguments of function of the form
  • It is a function body right arrow

Now, we use a lambda expression reconstruct the first section of the code


public static void main(String[] args) {
    LambdaDemo demo = new LambdaDemo();
    String something = "I am learning Lambda";
    //实现Printer接口(请关注下面这行lambda表达式代码)
    Printer printer = (String toPrint)->{System.out.println(toPrint);};
    //调用接口打印
    demo.printSomething(something, printer);
}

lambda expression to make our code more concise. In fact the use of lambda expressions in terms of performance and there are many more benefits of multi-core processing, but only after understanding java8 Streams API which makes sense, therefore, outside the scope of this discussion (before this number of articles have described).

Contrast implementation of traditional java code, the code amount is not reduced a lot? But this is still not the most simple implementation, we step by step.


Printer printer = (String toPrint)->{System.out.println(toPrint);};
//简化:去掉参数类型
Printer printer = (toPrint)->{System.out.println(toPrint);};
//简化:去掉参数括号
Printer printer = toPrint->{System.out.println(toPrint);};
//简化:去掉函数体花括号
Printer printer = toPrint->System.out.println(toPrint);
  • Not even the type of the designated parameter left arrow, the compiler will infer from the type of the formal parameter interface method
  • When only one parameter, we fully parentheses parameters can be omitted
  • When the body of the function is only one line, we can omit the function body braces

If we define the interface methods without any parameters, it can be replaced with empty parentheses:

()->  System.out.println("anything you wan to print")

So, we end up by lambda expressions, simplify the completion of the code is what like? True colors:


public static void main(String[] args) {
    LambdaDemo demo = new LambdaDemo();
    String something="I am Lambda";
    //关注下面的这行代码
    demo.printSomething(something, toPrint -> System.out.println(toPrint));
}

We used together as a function call within the lambda expression parameter, the first decline in 9 lines of code to the main method of only three lines. But I want to say, this is still not the lambda expressions can complete the ultimate way to simplify the code, when you learn java8 Stream API in conjunction with the use of lambda expressions, you will find that you will greatly increase coding efficiency!

in conclusion

Expression of the lambda expression is a function of the interface, the left arrow is a function of the parameters, the right arrow is a function thereof. Parameter types and return type of the function can be omitted, the program automatically determines the data type based on the context defined by the interface.

In this article, we are in Java Lambda expressions are introduced in detail and learn how to use them to improve the efficiency and quality of the interface. Please pay attention to this number more about the content, when used in conjunction with the Collections Stream API framework offers more advantages for Lambda.

Look forward to your attention

Guess you like

Origin www.cnblogs.com/zimug/p/11817433.html