JAVA8--Simple and elegant lambda expression

Source: Internet
Author: User

After the release of Java8, the words such as lambda expressions, stream, and so on are slowly appearing in our words. As Java7 appeared, everyone saw the "diamond grammar" and saw the Try-with-resource and so on. In the face of these new things, if it can provide convenience for us to bring about a different change. Then it's worth a taste of fresh. After the Java8 appeared, I glanced at the new thing. But jdk1.7,1,6 is commonly used in practical work. So it has been "idle". Now take a look at the idle.

What is a lambda expression?

The following will demonstrate the lambda expression in the form of code that requires the installation of JDK8. If your development tools are using Eclipse or myeclipse, be aware of the IDE's version. The low version does not support JAVA8.

  

How does a lambda expression create a thread?

We know that there are two ways of creating threads:

1. Write a class that inherits from the thread class and implements the Run method. Then, call the subclass's start () method to start the thread.

2. Implement the Runable interface and write the specific implementation in the Run method. The runable implementation class is passed as a parameter to the constructor of the thread class to complete the thread creation.

The second method can be implemented as follows:

       Example 1-1
Thread sayhello=new thread (new Runnable () { @Override publicvoid Run () { System.out.println ("Hello"); } }); Sayhello.start ();

The code above is simple, and the thread constructor receives an anonymous function. The anonymous function returns a runnable interface implementation. After the thread is started, it will output: Hello information.

The code in example 1-1 above would look like this if it were written in a lambda expression:

       Example 1-2
Thread thread=new thread ((), System.out.println ("Hello"); Thread.Start ();

Only two lines! What do you think? This point has no attraction to you. Example 1-2 is more concise and at a glance than the code in example 1-1. These changes lie in this line of code:

() System.out.println ("Hello")

This is a lambda expression. It is both a "parameter" and an "action". The first time you look at it, it will be very strange, but probably feel the effect of it, you can make an understanding of the whole action: () means that the implementation is a non-parameter method of no return value. The following is a sentence System.out.pringln ("Hello"), which is a concrete implementation of the method body. That's a good understanding. Let's look at an example below.

Using lambda expressions for file filtering

In the file operation, we can implement the Java.io.FileFilter class and write the code implementation of the filter file in its Accept method. This example assumes that there is a folder resource on the D drive in the Windows system, there are several files in it, and there are various formats such as. Jc,.html,.doc. Now you want to select the JS file. The implementation is as follows:

Example 2-1
//1. File directoryFile filedir=NewFile ("D:/resource"); //2. Create a filter RuleFileFilter filter=NewFileFilter () {@Override Public BooleanAccept (file file) {if(!file.isdirectory () &&file.getname (). EndsWith (". js"))){ return true; } return false; } }; //3. Get Filtered filesFile[] Files=Filedir.listfiles (filter);

In fact, the function is very simple, but a lot of code. If you are not writing, but you are reading someone else's code, I am afraid it is not a glance can be seen. You have to read these lines from top to bottom. However, if the lambda expression is written, it will look something like this:

Example 2-2     
// 1. File directory File filedir=new file ("D:/resource"); // 2. Filtering File [] Files=filedir.listfiles ((f)->!f.isdirectory () &&f.getname (). EndsWith (". js"));

What, does it look more concise?

(f)->!f.isdirectory () &&f.getname (). EndsWith (". js")

This line is on the table above 4, 5 lines of meaning. Because there is only one argument f in the parentheses, you can also omit the parentheses:

F->!f.isdirectory () &&f.getname (). EndsWith (". js")

Write it down here. The "looks" and "uses" of lambda expressions have been shown. Personal feeling is elegance. Just for the first time to see the words, may feel, a parenthesis, an arrow. What the hell.

But when you understand it, it seems to be pleasing to the eye. In the above two examples, we introduce the case of participating without parameter, return value and no return value. It's basically enough. When specific coding is used, it is better to write the implementation according to the specific logic. The lambda expression in the example is as follows in two ways:

() xxx (x, y)->x,y processing

As you can see, the structure is three parts, the parenthesis before the arrow is the method Head section, and the arrow is followed by the method body implementation. You can divert, extrapolate to write other types of lambda expressions.

1. How to understand lambda expressions.

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

2. Why the code in example 2-2, (f) parameter F parameter type is not, the general method is not all indicate the parameter type. This is not to say that f is a file type, why do not error it?

Answer: type inference.

Type inference for lambda expressions

Not finished: Updated later.

Java8--simple and elegant lambda expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.