It takes about 4 minutes to read this article.
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?
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.
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:
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.
Let’s go straight to the example.
Assume that the definition of Person and the value of List
Now you need to print the firstnames of all guiltyPersons whose lastnames start with “Z”.
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 () :
, 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.
Here we assume we have a Person Object and an Optional wrapper for the Person Object:
If not used in conjunction with Lambda, null check is not trivial.
Lambda+Optional
in Java 8 compared to traditional Java.
Case number one. – If it exists, go dry
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.
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