Lambda expression.

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

Lambda expression.

https://url.cn/544DLQl?sf=uri

https://it-xzy.github.io/JAVA-NEW/20181107-D-JAVA-10.html#/

Hello, I'm he Shuang, a student of the 11th phase of Zhengzhou branch of it Academy. I'm an honest, pure and kind back-end programmer. Today, I'd like to share with you the knowledge point in deep thinking - lambda expression, Java (profession) task 10 on the official website of the Academy.

1. Background introduction

1、 Introduction

(Note: although it looks very advanced, in fact, the essence of lambda expression is just a "syntax sugar". The compiler infers and converts it into regular code for you, so you can use less code to achieve the same function.

It is recommended not to use it indiscriminately, because it is as concise, difficult to understand and debug as the code written by some advanced hackers. The maintenance personnel want to curse their mother.)

Lambda expressions are an important new feature in Java se 8. Lambda expressions allow you to replace functional interfaces with expressions. Lambda expressions, like methods, provide a list of normal parameters and a body (which can be an expression or a code block) that uses these parameters.

Lambda expressions also enhance the collection library. Java se 8 adds two packages for batch operation of collection data: Java util. Function package and Java util. Stream package. A stream is like an iterator, but with many additional features.

In general, lambda expressions and streams are the biggest changes since the Java language added generics and annotations.

Lambda is actually an anonymous method; Java will quietly restore these into anonymous classes behind the scenes. Anonymous class -- refers to instantiating a class while declaring it, making the code more concise and concise.

Anonymous class?

Anonymous class refers to instantiating a class while declaring it to make the code more concise. Generally, to use an interface or abstract class, you must create a subclass. Sometimes, in order to use it quickly, you can directly instantiate an abstract class,

And implement its abstract method "on the spot". Since the abstract method is implemented, it is a new class, but this class is not named. Such classes are called anonymous classes.

2. Knowledge analysis

Lambda operator:

All lambda expressions use the new lambda operator "- >", which can be called "go" or "become".

Operator divides the expression into two parts. The input parameter is specified on the left, the arrow is in the middle, and the body of lambda is on the right.

(parameter type parameter name) - > {code body}

1. Standard writing method: (type 1 parameter name 1, type 2 parameter name 2...) - > {code block}

2. Parameter type can be inferred: (parameter name 1, parameter name 2...) - > {code block}

3. There are multiple lines of code: (parameter name 1, parameter name 2...) - > {code line; -- code line}

4. Single parameter and inferable type: (parameter name 1) - > {code block}

5. Method reference: system out::println

6. No parameters: () - > {code block}

Lambda expressions and anonymous inner classes:

Anonymous inner class is an inner class without a name. Because there is no name, anonymous inner class can only be used once. It is usually used to simplify code writing

However, there is another prerequisite for using anonymous inner classes: you must inherit a parent class or implement an interface. PS: lambda expressions cannot replace all anonymous inner classes, but can only be used to replace the abbreviation of functional interface.

Lambda expressions and functional interfaces:

A functional interface is an interface that has only one abstract method, but can have multiple non abstract methods.

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

Previously, they were called Sam types, that is, single abstract method types.

The method in the interface is public abstract by default.

Functional interface needs to be added: @ functionalinterface

3. Frequently asked questions

1. If lambda can only be used once, what are its advantages?

2. Why do we need lambda expressions?

4. Solutions

one

I Lamada expression simplifies the process of interface implementation and makes the code structure more concise.

II The use of lamada is very flexible.

two

Lambda expressions instantiate interfaces or classes by implementing anonymous classes. However, because the instantiated object has no name, it cannot be used twice. Therefore, lambda expressions can only be used once, that is, only one method can be used or executed.

Simply put, constructing a method entity with a lambda expression is equivalent to implementing the methods in the interface. The final return value of the lambda expression is the implementation class of the interface.

5. Coding practice

6. Expand thinking

Advantages of lambda:

Compared with the concept of anonymous class, lambda is actually an anonymous method, which is a programming idea of passing methods as parameters. The code is simple.

Does lambda have any shortcomings?

1. Poor readability. Compared with the verbose but clear anonymous class code structure, once the lambda expression becomes longer, it is difficult to understand.

2. It is not easy to debug. It is difficult to add debugging information, such as logs, to lambda expressions.

3. Version support. Lambda expressions are only supported in jdk8. If the system uses a previous version and is unwilling to upgrade due to the stability of the system, it cannot be used.

7. References

https://www.jianshu.com/p/4e858f2084b8

https://blog.csdn.net/KOKjock/article/details/80114900

https://www.cnblogs.com/franson-2016/p/5593080.html

8. More discussion

Q1: questioner wyq:

What are the disadvantages of lambda?

A1: respondent (H):

The readability of lambda expression is poor, which makes its later maintenance very difficult.

Q2: questioner: zhh

What does a lambda expression do?

A2: respondent (H):

The most intuitive function is to make the code extremely concise.

Q3: questioner: ZYQ

Where can I use lambda expressions?

A3: respondent (H):

You can use lambda expressions on the functional expression interface.

9. Acknowledgment

This tutorial is based on self searching materials.

10. Conclusion

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>