It takes about 4 minutes to read this article.

Author: Sevenvidia


https://www.zhihu.com/question/20125256/answer/324121308

What is the Lambda?

We know that we can assign a “value” to a Java variable.


If you want to assign a “block of code” to a Java variable, what do you do?

For example, I want to assign the block of code on the right to a Java variable called aBlockOfCode:
Before Java 8, this was not possible. But with Java 8, using the Lambda feature, you can do just that.

Of course, this is not a very neat way to write it. So, to make the assignment more elegant, we can remove some useless declarations.


Thus, we have successfully assigned a “block of code” to a variable in a very elegant way. And the “piece of code,” or “function assigned to a variable,” is a Lambda expression.

But there is still a question, what type should the variable aBlockOfCode be?

In Java 8, all Lambda types are interfaces, and the Lambda expression itself, the “code,” needs to be an implementation of that interface. This is what I think is a key to understanding Lambda, in short, that Lambda expressions themselves are implementations of interfaces. It might be a little confusing to say this directly, but let’s go ahead and look at some examples. Let’s add a type to the above aBlockOfCode:


This type of interface, in which only one function needs to be implemented, is called a functional interface. To prevent this interface from having multiple functions that need to be implemented, we can add a declaration @functionalInterface to this interface so that no one can add new functions to it:


Thus, we have a complete Lambda expression declaration:

What do Lambda expressions do?


The most obvious effect is to make code extremely concise.

We can compare Lambda expressions with a traditional Java implementation of the same interface:



So these two things are essentially equivalent. But Java 8 is clearly more elegant and concise. And since Lambda can be assigned directly to a variable, we can pass Lambda directly to a function as an argument, whereas traditional Java must have a well-defined interface implementation to initialize:

In some cases, this interface implementation only needs to be used once. Whereas traditional Java 7 would require you to define a “dirty” interface to implement MyInterfaceImpl, Java 8’s Lambda is much cleaner.

Lambda combines FunctionalInterface Lib, forEach, stream(), method Reference and other new features to make code more concise!

Let’s go straight to the example.

Assume that the definition of Person and the value of List are given.

Now you need to print the firstnames of all guiltyPersons whose lastnames start with “Z”.

Original Lambda writing: Define two functional interfaces, define a static function, call the static function and assign Lambda expressions to the parameters.

This code is actually cleaner, but could it be cleaner?

B: Sure. In Java 8, there is a functional interface package that defines a large number of functional interfaces that can be used (java.util.function (Java Platform SE 8)).

So, we don’t need to define NameChecker and Executor functional interfaces at all, You can just use the Java 8 functional interface package for Predicate

and Consumer

— the interfaces for these two are the same as those for NameChecker/Executor.



Step 1 Simplification – Using functional interface packages:




The for each loop inside a static function is an eyesore. We can use Iterable’s forEach() instead. ForEach () itself can accept a Consumer

argument.

Step 2 Simplify – replace forEach loop with iterable.foreach () :



Since the static function actually does one operation on the List, we can get rid of the static function and use the stream() property instead. Several methods of stream() accept arguments such as Predicate

, Consumer

, etc. (java.util.stream (Java Platform SE 8)). Now that you understand the above, stream() is pretty straightforward and doesn’t need much explanation.

Step 3 Simplification – replace static functions with stream() :

This is very, very neat compared to the Lambda we started with. Println (p -> system.out.println (p); You can use Method reference to further simplify. A Method reference is a Method that has already been written to replace the Lambda expression with another Object/Class Method. The format is as follows:



Step 4: Simplify – If println(p), we can use Method reference instead of Lambda expression in forEach:



This is basically the simplest version you can write.

Lambda in conjunction with Optional<T> can make Java’s handling of NULL extremely elegant


Here we assume we have a Person Object and an Optional wrapper for the Person Object:



Optional

If not used in conjunction with Lambda, null check is not trivial.



Its true power comes only when Optional<T> is used in conjunction with Lambda!


Lambda+Optional

in Java 8 compared to traditional Java.

Case number one. – If it exists, go dry

Case two – Return if it exists, return fart if it does not

Case 3 – returns if it exists, or if it does not



Case 4 – Kill chain null check

It is clear from the above four cases that Optional

+Lambda will save us a lot of ifElse blocks. The new Optional

+Lambda is clean and concise, while the traditional Java method is verbose and confusing.

There are still things to discuss and learn about Lambda in Java. For example, how to handle a lambda Exception, how to use lambda features for parallel processing, etc.


Anyway, I’m just giving you the general idea, as always, to give you the general idea, oh! This is how it used to be. There are many online tutorials on Lambda, read and practice. Over time, there must be some lean.


, END,

The growth path of programmers

Though the road is long, the journey is sure to come

This article was originally posted on the wechat public account of the same name “The Growth of programmers”, reply to “1024” you know, give a thumbs up.

Reply [520] to receive the best learning method for programmers

Reply to [256] for Java programmer growth plans