About the use and understanding of Lambda expressions

Hello everyone! At 20:30 Beijing time, on the 21st day of working from home, I will give you a popular science about Lambda expressions to help you and pass the time.

 

Before learning this, we need to know what can be done by learning this, what is the use, and whether it is very useful... In fact, if it is useful, it has no effect at all without lambda. The main purpose of lambda is to make our code look good. There are several advantages:

1. Avoid too many definitions of anonymous inner classes
. 2. It can make your code look very concise.
3. Remove a bunch of meaningless codes, leaving only the core logic.

If it is just a verbal description, it is difficult for me to make everyone understand deeply. Next, I will use the code to let everyone see more clearly the sharp contrast between not using lambda expressions and using them. Before showing the code, everyone needs to know a prerequisite. that is:

Understanding FunctionalInterface (functional interface) is the key to learning Java8 lambda expressions.

Definition of a functional interface:

Any interface that contains only one abstract method is a functional interface.

Next, please look at the code. I will reduce the amount of code again and again to reflect the excellence of lambda:

First we define a functional interface:

interface lambdahappy{
    void lambda();
}

Next is his implementation class, we output a sentence in it:

class happy implements  lambdahappy{
        @Override
        public void lambda() {
        System.out.println("meet lambda so Happy!");
    }
}

Then we use our most common method to call this class new object:

    public static void main(String[] args) {
        lambdahappy happy = new happy();
        happy.lambda();
}

The console successfully prints out this sentence. This is the most common method. Now we use a static inner class to call, and also output a sentence:

static class happy2 implements  lambdahappy{
        @Override
        public void lambda() {
            System.out.println("meet lambda so Happy2!");
        }
    }

Successfully printed, then we use the local internal class, the local internal class is simply to change the location, just put this method in the main method, and output a sentence like this:

    class happy3 implements  lambdahappy{
            @Override
            public void lambda() {
                System.out.println("meet lambda so Happy3!");
            }
        }

Next, we use anonymous inner classes, which have no class name and must use interfaces or parent classes:

lambdahappy =new happy(){
            @Override
            public void lambda() {
                System.out.println("meet lambda so Happy4!");
            }
        };

The output is successful, let's try it with lambda:

 lambdahappy =() ->{
            System.out.println("meet lambda so Happy5!");
        };

You will find that the lambda expression omits the steps of instantiating objects and calling methods. These steps can be completed in one simple step. Compared with the above output method, does lambda look more concise? Next, I will give you a demonstration. , if there are parameters in the interface, how to simplify it, including the specific degree to which it can be simplified, please see the following code display:

First, let's start with a functional interface:

interface cool{
    void cool(String a);
}
public class kambdaTest2 {


    public static void main(String[] args) {

        // 1.lambda表示简化
        cool cool = (String a)->{
                System.out.println("meet lambda so cool!"+a);
            };

        //简化去掉参数类型
        cool=(a)->{
            System.out.println("meet lambda so cool!"+a);
        };

        //简化去掉括号
        cool=a->{
            System.out.println("meet lambda so cool!"+a);
        };

        //简化去掉大括号
        cool=a->System.out.println("meet lambda so cool!"+a);
        
        cool.cool("jarry");
    }
}

 Do you see the pattern? Through the simplification process of the above steps, we can use only one sentence to make an output call, but there are a few things to pay attention to, let me summarize for you:

1. The premise is that the interface is a functional interface (there is only one method in the interface)

2. A lambda expression can only be simplified to one line if it has one line of code. If there are multiple lines, then wrap it with a code block. 3.
Multiple parameter types can also be removed. If you want to remove all of them, you must add parentheses .

Finally, I attach a night scene I just took. I named him 'I haven't seen the stars for a long time'. I hope the above content can help everyone. I wish you safety and health, love life, and love code!

 

 

 

Guess you like

Origin blog.csdn.net/m0_67864787/article/details/127622909