What is a lambda expression?
1. an anonymous function;
2. indirectly to the interface.
. 1 / ** 2 * 1.lambda - an anonymous function 3 * 2 can realize a simple interface 4 * 3 abstract methods defined in the interface must be a requirement . 5 * / . 6 public class Demo1 { . 7 public static void main (String [] args) { . 8 // implementation of that interface . 9 Comparator Comparator = new new MyComparator (); 10 . 11 // anonymous inner classes 12 is Comparator comparator2 = new new Comparator () { 13 is @Override 14 public int Compare ( int a, int b) { 15 return a - b; 16 } 17 }; 18 19 //lambda 20 Comparator comparator3 = (a,b) -> a - b; 21 } 22 } 23 24 class MyComparator implements Comparator{ 25 26 @Override 27 public int compare(int a, int b) { 28 return a - b; 29 } 30 } 31 is 32 / ** 33 * abstract method only a modified function interface 34 is * / 35 @FunctionalInterface 36 interface Comparator { 37 [ int Compare ( int A, int B); 38 is }
Use several basic forms:
1 public class BasicUse { 2 public static void main(String[] args) { 3 //() --- 参数列表 4 //{} --- 方法体 5 // -> --- lambda运算符 goes to 6 NoReturnAndParam noReturnAndParam = () ->{ 7 System.out.println("Hello lambda"); 8 }; 9 noReturnAndParam.f(); 10 11 NoReturnSingleParam noReturnSingleParam = (int a) ->{ 12 System.out.println(a); 13 is }; 14 noReturnSingleParam.f (100 ); 15 / ** 16 * a single parameter may be omitted brackets 17 *, and only one may be omitted statement 18 is * / . 19 NoReturnSingleParam noReturnSingleParam2 = A -> System.out.println (A); 20 is 21 is noReturnMutiParam noReturnMutiParam = ( int A, int B) -> { 22 is System.out.println (A + B); 23 is }; 24 noReturnMutiParam.f (10,100 ); 25 / ** 26 is * Interface type defines the parameters, parameter types may be omitted, but must be omitted altogether 27 * Parameter Type Compact 28 * / 29 NoReturnMutiParam noReturnMutiParam2 = (A, B) -> { 30 System.out.println (A + B); 31 is }; 32 33 is returnAndNoParam returnAndNoParam = () -> { 34 is System.out.println ( "noParamWithReturn" ); 35 return 10 ; 36 }; 37 [ int Re = returnAndNoParam.f (); 38 is System.out.println (Re ); 39 40 ReturnAndSingleParam = ReturnAndSingleParam ( int A) -> { 41 is return A * 100 ; 42 is }; 43 is int reParam = returnAndSingleParam.f (20 is ); 44 is System.out.println (reParam); 45 / ** 46 is * When a statement is only a when return, in addition to further omit braces omitted return 47 * / 48 ReturnAndSingleParam returnAndSingleParam2 = A -> A * 10 ; 49 50 ReturnAndMutiParam returnAndMutiParam = ( int A, int B) -> { 51 is return A - b; 52 }; 53 int reParams = returnAndMutiParam.f(100,20); 54 System.out.println(reParams); 55 56 } 57 }
Method references
. 1 public class MethodUse { 2 public static void main (String [] args) { . 3 // method references . 4 / ** . 5 * the lambda expression to implement a function that has been achieved 6 * by ordinary method 7 * class / object 8 * Method type and number of parameters and the interface must be the corresponding 9 * return value must be the same 10 * / . 11 ReturnAndSingleParam returnAndSingleParam = A -> Change (A); 12 is 13 is ReturnAndSingleParam returnAndSingleParam2 = MethodUse :: Change; 14 } 15 Private static int change(int a){ 16 return a * 10; 17 } 18 }
Constructor references
. 1 public class ConstructorUse { 2 public static void main (String [] args) { . 3 PersonUse personUse = () -> new new the Person (); . 4 // reference constructor with no arguments . 5 PersonUse personUse2 the Person :: = new new ; . 6 the Person Person = personUse2.getPerson (); . 7 System.out.println (Person); . 8 . 9 // references have constructor parameter 10 PersonUse2 personUse21 the Person :: = new new ; . 11 the Person person21 = personUse21.getPersonWithParam ( "Dave", 26 is); 12 System.out.println(person21); 13 } 14 } 15 interface PersonUse{ 16 Person getPerson(); 17 } 18 19 interface PersonUse2{ 20 Person getPersonWithParam(String name,int age); 21 }
Examples of thread
1 public class ThreadUse { 2 public static void main(String[] args) { 3 /** 4 * Runnable接口,无参无返回 5 */ 6 Thread t1 = new Thread(() ->{ 7 for (int i = 0; i < 10 ; i++) { 8 System.out.println(i); 9 } 10 }); 11 t1.start(); 12 } 13 }
Closure
. 1 public class CloserDemo { 2 public static void main (String [] args) { . 3 int n-= the getNumber () GET ();. . 4 System.out.println (n-); . 5 } . 6 . 7 / ** . 8 * Closure ---> Get a local variable in the method of 9 * reference a local variable ---> add default constant final 10 * @return . 11 * / 12 is Private static Supplier <Integer> the getNumber () { 13 is // lift lifecycle local variables, you can still be used after the end of method 14 int NUM = 10 ; 15 return () -> { 16 return num; 17 }; 18 } 19 }