JAVA9 all to come out, JAVA8 new features are not clear, is not a bit behind OH ~
LAMDA expression, read as lambda expression , it is essentially a concept of functional programming, to understand the purpose of functional programming, we must first understand the anonymous inner class.
Let's take a look at the traditional method of anonymous internal class invocation:
Interfacemyinterface{voidLmethod ();} Public classMain { Public Static voidTest (MyInterface myinterface) {Myinterface.lmethod (); } Public Static voidMain (string[] args) {Test (NewMyInterface () {@Override Public voidLmethod () {System.out.println ("Hello world!"); } }); }}
In a few lines of code in the main class, nesting layers just to output a Hello world! Is it a lot of trouble? But because of the integrity of the Java structure, we had to do it, and now JDK1.8.
Let's take a look at using the LAMDA expression to rewrite the above code:
Interface myinterface{ void Lmethod ();} Public class Main { publicstaticvoid Test (MyInterface myinterface) { Myinterface.lmethod (); } Public Static void Main (string[] args) { test (()->system.out.println ("Hello world!" )); }}
This is the LAMDA expression language, in order to solve the anonymous internal class cumbersome operation.
There are three forms of LAMDA syntax:
- (parameter), single-line statement;
- (parameter)->{multi-line statement};
- (parameter)-expression;
Parentheses () can be roughly understood as the method, which is the parameter variable, in the above example ()->system.out.println ("Hello world!") in front of the () means the Void Lmethod () method, it does not enter the parameter, so it is empty,-> followed by a single line statement;
If it is followed by a multiline statement, it needs to be loaded with {}, and a semicolon is required after each statement;
The following can also be an expression, such as: A+b.
Single-line statement (parameters):
Interfacemyinterface{voidLmethod (String str);} Public classMain { Public Static voidTest (MyInterface myinterface) {Myinterface.lmethod ("Hello world!");//Setting parameter Contents } Public Static voidMain (string[] args) {//This expression is first defined in () to receive the variable s, which can be used in the following single-line statementTest (s)System.out.println (s)); }}
(parameter)->{multi-line statement}:
Interfacemyinterface{voidLmethod (String str);} Public classMain { Public Static voidTest (MyInterface myinterface) {Myinterface.lmethod ("Hello world!");//Setting parameter Contents } Public Static voidMain (string[] args) {//This expression is first defined in () to receive the variable s, which can be used in subsequent multiline statements. Note: Multiple lines of statement ";" NumberTest (s){s=s+s; System.out.println (s); }); }}
(parameters)-expression:
Interfacemyinterface{intLmethod (intAintb);} Public classMain { Public Static voidTest (MyInterface myinterface) {int(Result=myinterface.lmethod);//Set parameter contents, receive return ParametersSystem.out.println (Result); } Public Static voidMain (string[] args) {test (x, y)X*y);//Calling Methods//equivalent//Test ((x, y) {return x*y;}); }}
In this way, the LAMDA expression looks very simple, there is no!
Anonymous inner class, where are our more commonly used places? Threading class Thread, which we might have previously written:
New Thread (New Runnable () {
@Override
public void Run () {
SYSTEM.OUT.PRINTLN ("Thread operation! ");
}
});
Now, using the LAMDA expression, simply write:
New Thread (()->system.out.println ("Threading Operation! "));
Summary: The LAMDA expression is used to avoid an anonymous inner class that defines too many useless operations.
JAVA8 new feature--LAMDA expression