Lambda expressions
Functional programming ideas outlined
what --- emphasis on doing rather than what form do
Object-oriented thinking:
- Way to do one thing, looking for a target to solve this thing, call the object, to get things done
Functional programming ideas
- As long as get to the results, who do, do so not important, important is the result, not pay attention to the process
Traditional writing and writing contrast Lambda
Traditional writing
class public CaiNiao {
public static void main (String [] args) {
// anonymous inner classes, of multithreading
// = the Runnable new new Task the Runnable ()
the Runnable new new Task = the Runnable () {
@Override
public void RUN () {/ / overwrite abstract method
System.out.println (. Thread.currentThread () getName ( ) + " to create a new thread" ); System.out.println ( "multithreaded execution!" );
} }; new new the thread (task) .start (); // open thread }}
The benefits and drawbacks of an anonymous inner class
- On the one hand, an anonymous inner classes can help us achieve omit the definition of the class;
- On the other hand, the syntax of anonymous inner classes really too complicated
class public CaiNiao {
public static void main (String [] args) {
// anonymous inner classes, of multithreading
// = the Runnable new new Task the Runnable ()
new new the Thread (new new the Runnable () {
@Override
public void RUN () {/ / overwrite abstract method
System.out.println (. Thread.currentThread () getName ( ) + " to create a new thread" ); System.out.println ( "multithreaded execution!" ); } }) Start. (); // open thread }}
Lambda wording
()->{}
class public CaiNiao {
public static void main (String [] args) {
// anonymous inner classes, of multithreading
// = the Runnable new new Task the Runnable ()
new new the Thread (() -> {// overwrite abstract methods
System. out.println (. Thread.currentThread () getName ( ) + " to create a new thread" );
System.out.println ( "multithreaded execution!" ); } ) .start (); // open thread } }
Lambda expressions standard format
It consists of three parts:
- Some parameters
- An arrow
- A piece of code
format:
- (Parameter list) -> {} Code number rewriting process:
Explanation format:
- (): The list of interfaces abstract method parameters, no parameters, it is empty
- ->: transfer means, the parameters passed to the method body {}
- {}: Abstract method override interface method thereof
Lambda expressions: is derived, may be omitted
- Any content derived from the context, the writing can be omitted
Content can be omitted :
- (Parameter list): a list of parameters in parentheses data type, can be omitted
- (Parameter list): brackets if only one parameter, then the type and () can be omitted
- (Some code): if () code in only one row, regardless of whether the return value may be omitted ({}, return, semicolon)
NOTE: To omit {}, return, semicolon must be omitted altogether
- Before JDK1.7 version, create a collection of generic objects must be written on the front and rear
- After JDK1.7 version number = generic back may be omitted, the back can be derived according to the generic generic front
new Thread (() -> { // overwrite abstract method
System.out.println (Thread.currentThread () getName () + " created the new thread." );
}
) .start (); // open thread
new thread (. () -> System.out.println (Thread.currentThread () getName () + " to create a new thread")) start (); // open thread.
Lambda premise of use
- Using a Lambda must have an interface, and the interface and only requires an abstract method;
- Using a Lambda must be inferred context.
Note: There is one and only one abstract method interface becomes "function interface."