Tips for Answering a Lamdba Interview

Jdk8 is not a new thing, but in general, JDK8 is a big change, adding a lot of new things, such as Lamdba, StreamApi, new time processing methods, and new changes in the interface. Among them, Lamdba is the more novel one. The weird grammar of lamdba seems a bit baffling at first glance. So what is lamdba?

 

Anonymous delegate (anonymous inner class)

 

I didn't plan to explain the concept of lamdba in detail on Baidu. First, I have a general understanding of what it is useful for and how it is good for us, and then it is up to you to decide whether to use lamdba in your future work.

Where to start? Let's start with the anonymous delegates that our workgroups often use...

What is anonymous delegation? In fact, it is the "anonymous inner class" we often use

For example, the thread we use wonderfully, there are several ways to create a thread, so use the traditional one.

 

  new Thread(new Runnable() {

            @Override

            public void run() {

                System.out.println("Well, yes, this is an anonymous inner class");

            }

        }).start();

 

 

The above writing method should be familiar to you. If you are not familiar with this, then the guest officer should lay the foundation of java first.

Take a closer look at the above, the above code is a screenshot from IDEA. If you look closely, we find that there is a gray wavy line on it. When we put the mouse on the gray one, we will find it.

 

 

Translation: Anonymous inner classes can be replaced by Lambda expressions.

So how to use lamdba expression instead?

 

Fantastic Lamdba

 

I won't talk about the grammar of Lamdba, let's replace the "anonymous inner class" above.

new Thread(()->System.out.println("Well, yes, this is an anonymous inner class")).start();

 

 

Compared with the code of the anonymous inner class above, do you find that the code is missing many lines, and there is a lamdba symbol in the IDEA tool.

Well, I confirm that this is the lamdba expression, then you should be able to see the significant advantage of lamdba "makes the code more concise".

 

Definition of Lamdba in Vernacular

 

Well, after looking at the simple case above, I will ask you a question. Suppose I am your interviewer now, and when you come for an interview, I will ask you a question.

"Have you heard of Lamdba? Please tell me what Lamdba is?" How would you answer?

The official explanation, please go to Baidu yourself, I explain Lamdba as follows

 

Lambda is an anonymous function, that is, a function without a function name ( simplifies the use of anonymous delegates, allowing you to make code more concise )

 

It should be understandable that Lamdba is an anonymous function. By rewriting Runnable above, you can find that it is actually another way of writing the Runnable interface (just a little weird)

More importantly, the content marked in red font simplifies the code. Well, I think you should be able to feel this intuitively, yes, if the interviewer asks you, can you retell the above case out?

Through this article, I think you have understood what lamdba is, so what is the syntax of lamdba?

I can write anonymous inner classes now, how can I change them into Lambda expressions according to the anonymous inner classes I wrote?

If you want to know the following, please listen to the next analysis, and if you want to read other articles, please leave a message~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325229450&siteId=291194637