The of Java 8 — simple and elegant lambda expressions to explore Java 9 module system and reaction flow

After the release of Java 8, words such as lambda expression, stream and so on slowly appeared in our words. For example, after the emergence of Java 7, we saw "Diamond syntax", try with resource and so on. Facing these new things, if it can provide convenience for us, it will bring different changes. Then it's worth trying. After the advent of Java 8, I glanced at new things. However, jdk1 is widely used in practical work 7,1,6。 So it has been "idle". Now I'm free to have a look.

What is a lambda expression like?

The following will demonstrate the lambda expression in the form of code. You need to install jdk8.0 first If the development tool uses eclipse or MyEclipse, you should also pay attention to the version of IDE. Earlier versions do not support Java 8.

How do lambda expressions create a thread?

We know that there are two ways to create threads:

1. Write a class that inherits from the thread class and implements the run method. Then, the start () method of the subclass is invoked to start the thread.

2. Implement the runable interface and write the specific implementation in the run method. Pass the implementation class of runable as a parameter to the constructor of thread class to complete the creation of thread.

The implementation of the second method can be written as follows:

The above code is very simple. An anonymous function is received in the thread constructor. The anonymous function returns a runnable interface implementation. After starting the thread, it will output: Hello information.

If the code in example 1-1 above is written in lambda expression, it will look like the following:

Only two lines! What about? Does this attract you. Compared with the code in example 1-1, example 1-2 is more concise and clear at a glance. These changes are in this line of code:

This is a lambda expression. It is not only a "parameter", but also an "action". It's strange to see it for the first time, but you can understand the whole action by roughly feeling its function: () indicates that the implementation is a non parametric method. - > The following sentence points to system out. pringln("hello"); This code is the concrete implementation of the method body, only one sentence is printed. This understanding is very smooth. Let's take another example.

File filtering using lambda expressions

In the file operation, we can implement Java io. Filefilter class and write the code implementation of filtering files in its accept method. This example assumes that in the windows system, disk D has a folder resource with some files and jc,. html,. Doc and other formats. Now select the JS file. The implementation is as follows:

In fact, the function is very simple, but there is a lot of code. If you're not writing it, but reading someone else's code, I'm afraid you can't see it at a glance. I have to read these lines from top to bottom. However, if you write a lambda expression, it will look like this:

Well, does it look much more concise.

(f)->! f.isDirectory()&&f.getName(). The line endswith (". JS") shows the meaning of the above 4 and 5 lines. Since there is only one parameter f in parentheses, parentheses can also be omitted:

f->! f.isDirectory()&&f.getName(). endsWith(".js")

Write here. The "appearance" and "purpose" of lambda expressions have been shown. Personal feeling is elegant. Just for the first time, you may feel, a bracket, an arrow. What is it?

But when you understand, it will look good. The above two examples respectively introduce the demonstration of participation, non participation, return value and no return value. Basically enough. When using specific coding, just write and implement according to specific logic. The lambda expressions in the example are as follows:

As you can see, the structure is divided into three parts. The bracket before the arrow is the method header, and the method body implementation is behind the arrow. Other types of lambda expressions can be written according to gourd painting and drawing inferences from one instance.

1. How to understand lambda expressions.

A: it's actually a line of code passed as a parameter. It is both a parameter and a set of code with "action".

2. Why does the code in example 2-2 have no parameter (f) and parameter type? Don't the general methods indicate the parameter type. It is not said here that f is of file type. Why not report an error?

Answer: type inference.

Recommended reading

[explore Java 9 module systems and reaction flows]( https://www.cnblogs.com/demingblog/p/9104103.html )

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
分享
二维码
< <上一篇
下一篇>>