Quickly familiarize yourself with Lambda expressions

There is a tool in the Java basic course that is very easy to use, but it is easy to forget if you don't use it frequently. You must have guessed that it is Lambda expression, a tool that is not difficult for anyone to learn. Today we will quickly familiarize ourselves with Lambda expressions.

1. Coding habits without Lambda expressions

Let’s talk about a simple interview question first. What is the relationship between anonymous inner classes and polymorphism? I believe everyone will have figured out the internal class knowledge points in the basic part of Java. The answer to this interview question is very simple. Anonymous inner classes are a very intuitive manifestation of polymorphism. Let's look at this example:

Runnable runnable = newRunnable() {
   
         @Override      public void run() {
   
             //线程体          System.out.println("lambda");      }  };

Runnable is the upper-level interface of the thread class. The run method of the Runnbale interface must be rewritten by the thread, also called the thread body. We are accustomed to using the above writing method when using Runnable to start threads, which is also an intuitive manifestation of polymorphism. But is there an easier way? Lambda expressions come to show off.

Runnable runnable = () -> System.out.println("Lambda");

2. Functional interface

Lambda expressions are actually objects of implementation classes of functional interfaces. Therefore, to understand Lambda expressions, we must first quickly understand what a functional interface is. ​​​​​​​

@FunctionalInterfacepublic interface Runnable {
   
       /**     * Runs this operation.     */    void run();}

This is the source code of the Runnable interface, which is a functional interface. We see several characteristics:

  • An interface with only one abstract method

  • Use the @FuncationInterface annotation to identify

3.Lambda expression implementation details

After knowing what a functional interface is, we can understand this sentence again: "Lambda expressions are actually objects of the implementation class of functional interfaces." At this point we can understand that using Lambda is actually creating an object of the implementation class of the functional interface.

Taking Section 1 as an example, let’s look at the evolution of obtaining objects through Lambda:

  • First get the parameter list of the only abstract method of the function, and use arrow symbols (also called Lambda operators) to separate the subsequent content.

Runnable runnable = ()-> {
   
           //线程体        System.out.println("lambda");    }};

The "()" on the left side of the arrow symbol is actually the declaration part of the "public void run()" method. This bracket describes the parameter list of the run() method, and the method name, return value type, and permission list are omitted. Because the interface has only one abstract method, these are automatically recognized by the compiler.

  • Simplify what's to the right of the arrow symbol:

If there is only one line of code, you can simply omit the curly braces.

Runnable runnable = () -> System.out.println("Lambda");

If there is only one line of code and there is a return, either the curly braces and the return must be omitted, or neither can be omitted. The following uses Lambda expression to create a compator interface object as an example.

//普通操作Comparator<Integer> comparator1 = new Comparator<Integer>() {
   
       @Override    public int compare(Integer o1, Integer o2) {
   
           return o1-o2;    }};//使用Lambda表达式创建Comparator<Integer> comparator2 = (o1, o2) -> o1-o2;

As can be quickly seen from the above example, the left side of the arrow symbol is the parameter list of the method, and the right side of the arrow symbol is the method body. Since there is only one line of code, there is no problem in omitting both the curly braces and return.

Okay, students, it’s that simple to use Lambda expressions to simplify the code.

For more exciting tutorials, please search "Qianfeng Education" on Station B

Qianfeng Education MySQL database tutorial, mysql installation to mysql advanced set of clearance

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/135268007