Introduction to lambda expressions
Lambda expressions are defined in the Java specification proposal JSR 335, which introduces lambda expressions in Java 8 and is considered to be the largest new feature in Java 8, which facilitates functional programming and simplifies Java programming development.
Background Knowledge anonymous inner class
In Java, anonymous inner classes are typically used for implementations that only occur once in a Java application, for example, there are many keyboard and mouse event handlers in standard swing or javafx applications, and generally you don't need to write a separate event-handling class, Instead, use the following methods (you should be familiar with swing programming experience):
New JButton ("Test button"); Testbutton.addactionlistener (new ActionListener () { @Override public void actionperformed (ActionEvent ae) { System.out.println ("click detected by Anon Class "); } );
If you do not, you need to write a separate implementation class for each event ActionListener interface, which is not elegant, because just to define a method you need to add a lot of code to write. Typically, where needed, the corresponding anonymous inner class is created, which makes the code more readable.
Functional Interfaces-Function interface
First, let's look at the definition of the ActionListener interface mentioned above, as follows:
Package java.awt.event; Import Java.util.EventListener; Public Interface extends EventListener { /** * Invoked when an action occurs. */ Public void actionperformed (ActionEvent e);}
In the above code, only one method is defined, in Java 8, an interface like this is called "Functional Interface". Usually in Java, we often use anonymous internal classes to implement functional interfaces, which is a very common usage pattern;
In addition, in the JDK, in addition to the ActionListener interface, there are similar runnable and comparator interfaces.
Lambda expression syntax
The syntax of a lambda expression is as follows:
parameter, expression body
Main composition: parameter list + arrow + expression body , e.g. X + y (int x, int y)
Where the expression body can be an expression, or it can be a block of statements (multiple code statements);
A lambda expression has the following characteristics:
- "Optional" type declaration: The type of the parameter does not need to be declared, the compiler can infer its type according to the parameter value;
- "Optional" brackets: If you have a single argument, you do not need to enclose the parameter in parentheses, although, of course, for multiple parameters or no parameters, parentheses are required;
- "Optional" curly braces: If the expression body has only one statement, do not need to surround with curly braces, of course, for multiple statements, curly braces are required;
- "Optional" return keyword: If the expression body is a single expression, the return keyword can not be written, the compiler can automatically return the value, of course, if you write a return, you need to add curly braces;
As an example:
PackageLambda;/*** Lambdatest * *@authorPi Chen *@versionV1.0.0, September 19, 2016 *@see * @sinceV1.0.0*/ Public classlambdatest{ Public Static voidMain (String args[]) {lambdatest tester=Newlambdatest (); //There are parameter typesmathoperation addition = (intAintb) A +b; //no parameter typeMathoperation subtraction = (A, b) A-b; //with curly braces and a return keywordMathoperation multiplication = (intAintb) { returnAb; }; //No curly braces, no return keyword, single-expression caseMathoperation Division = (intAintb) A/b; //Mathoperation Invocation ExampleSystem.out.println ("ten + 5 =" + Tester.operate (10, 5, addition)); System.out.println ("10-5 =" + tester.operate (10, 5, subtraction)); System.out.println ("Ten x 5 =" + Tester.operate (10, 5, multiplication)); System.out.println ("10/5 =" + tester.operate (10, 5, Division)); //with parenthesesGreetingservice GreetService1 = message, System.out.println ("Hello" +message); //no brackets, single parameter conditionGreetingservice GreetService2 = (message), System.out.println ("Hello" +message); //Greetingservice Invocation ExampleGreetservice1.saymessage ("Mahesh"); Greetservice2.saymessage ("Suresh"); //There are brackets, no reference conditionsRunnable runtest = (), System.out.println ("Running"); //runnable Invocation ExampleRuntest.run (); } //Internal Interface Interfacemathoperation {intOperation (intAintb); } InterfaceGreetingservice {voidsaymessage (String message); } InterfaceRunnable {voidrun (); } Private intOperateintAintB, Mathoperation mathoperation) { returnMathoperation.operation (A, b); }}
Key point Description:
- Lambda expressions are primarily used to define an inline implementation of a functional interface (functional interface: an interface that contains only one method), in the example above, We used various types of lambda expressions to implement the operation method of the Mathoperation interface, and then implemented the Saymessage method of the Greetingservice interface,Runnable The Run method of the interface;
- Lambda expressions eliminate the use of anonymous classes and give Java simple and powerful functional programming capabilities;
Resources
Http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html#overview
Https://www.tutorialspoint.com/java8/java8_lambda_expressions.htm
JAVA 8 Lambda expression-lambda Expressions