Java Learning: Lambda Expressions

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:

  1. Some parameters
  2. An arrow
  3. 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 :

  1. (Parameter list): a list of parameters in parentheses data type, can be omitted
  2. (Parameter list): brackets if only one parameter, then the type and () can be omitted
  3. (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

  1. Using a Lambda must have an interface, and the interface and only requires an abstract method;
  2. Using a Lambda must be inferred context.

Note: There is one and only one abstract method interface becomes "function interface."

Guess you like

Origin www.cnblogs.com/cainiao-chuanqi/p/11300138.html