Reference: https://www.bilibili.com/video/av50530058/?p=1
1. Lambda expressions are what?
: Lambda is an anonymous function (anonymous achieve an interface);
2.Lambda composition?
: Method body parameter list +
@ (): Argument list // {}: Method body // ->: Operator (Goes to)
3. Lambda basic grammar?
(Here are a few to achieve well-defined interfaces, method names are test, according to the notes on the code can probably understand what that means, do not write the interface here)
// No No parameter returns LambdaNoReturnNoParam lambda1 = () -> { // method body implementation method System.out.println ( "None None Return Value parameter!" ); }; Lambda1.test (); // no return a single parameter LambdaNoReturnOneParam lambda2 = ( int A) -> { System.out.println (A +. 1 ); }; lambda2.test ( 2 ); // no return multiple parameters LambdaNoReturnMutipleParam lambda3 = ( int A, int B) -> { System.out.println (A + B); }; Lambda3.test ( 10,20 ); // return a value of no parameter LambdaReturnNoParam lambda4 = () -> { return 12345 ; }; int RET = lambda4.test (); System.out.println (RET); / / return a single value of the parameter LambdaReturnOneParam lambda5 = ( int A) -> { return A +. 1 ; }; System.out.println (lambda5.test ( 44 is )); // return a plurality of parameter values LambdaReturnMutipleParam lambda6 = ( int A, int b) -> { return a * b; }; System.out.println(lambda6.test(3,6));
operation result:
No return value no arguments!
. 3
30
12345
45
18 is
4. Lambda expressions streamlined?
/ ** * streamline the Lambda expression syntax * 1. parameter type can be omitted; * 2. When a parameter only when the parentheses can be omitted * 3. If the method body is only a statement, the braces can be omitted * If the only the statement is a return statement, the curly braces and return should also be omitted * / public class Syntax2 { public static void main (String [] args) { LambdaReturnMutipleParam the lambda = (a, B) -> a * B; LambdaReturnOneParam lambda2 = a -> A +. 1 ; System.out.println (lambda.test ( 4,5 )); System.out.println (lambda2.test ( . 19 )); } }
5. Lambda expression method references?
/ ** * Method Reference: * quickly will realize a Lambda expression of a point to already-implemented method. * Syntax: Membership ::'s method method name * * Note: The number of parameters, types, return type, to consistent and defined in the interface. * / public class Syntax3 { public static void main (String [] args) { LambdaReturnOneParam lambda1 = a -> change (a); // references change implemented method. Lambda2 = LambdaReturnOneParam Syntax3 :: Change; } public static int Change ( int A) { return A + 2 ; } }
6. Lambda reference constructor
public class Syntax4 { public static void main(String[] args) { PersonCreater creater = () -> new Person(); //构造方法的引用: PersonCreater creater1 = Person::new; Person p = creater1.getPerson(); PersonCreater1 creater2 = Person::new; Person b = creater2.getPerson("小明",12); } } interface PersonCreater{ Person getPerson(); } interface PersonCreater1{ Person getPerson(String name,int age); }
7. Application Lambda expressions?
7.1 ordering of the collection
public class Exercase1 { public static void main(String[] args) { List<Person> list = new ArrayList<>(); list.add(new Person("Coco",12)); list.add(new Person("Tina",13)); list.add(new Person("Marry",10)); list.add(new Person("Lucy",16)); list.add(new Person("Nana",8)); list.sort((p1,p2) -> p1.age - p2.age); System.out.println(list); } }
public class Exercise2 { public static void main (String [] args) { / * SET internal sequencer, there is a need for a reference configuration parameter Comparator, so here it provides a comparison rule object. Using lambda expression is achieved Comparator interface, and instantiate a TreeSet objects * / // TreeSet <the Person> SET = new new TreeSet <> ((P1, P2) -> p1.age - p2.age); TreeSet <the Person> = SET new new TreeSet <> ((P1, P2) -> { IF (p1.age> = p2.age) { return . 1 ; } the else { return -1 ; } }); set.add (new Person("Coco",12)); set.add(new Person("Tina",13)); set.add(new Person("Marry",10)); set.add(new Person("Lucy",10)); set.add(new Person("Nana",8)); System.out.println(set); } }
7.2 traversing collection
public class exercise3 { public static void main (String [] args) { // List <Integer> = List new new the ArrayList <> (); Collections.addAll (List, 1,2,3,4,5,6,7, 8,9,0 ); / * the set to each element into the accept method, the configuration of a Consumer objects * / // list.forEach (the System.out :: the println); // output the set of all even list.forEach (ELE -> { IF (ELE% 2 == 0 ) { System.out.println (ELE); } }); } }
/ ** * Remove geometric elements satisfying the condition * / public class Exercise4 { public static void main (String [] args) { List List = <the Person> new new the ArrayList <> (); List.add ( new new the Person ( "Coco ", 12 is )); List.add ( new new the Person (" Tina ", 13 is )); List.add ( new new the Person (" Marry ", 10 )); List.add ( new new the Person (" Lucy ", 16 )) ; list.add ( new new the Person ( "Nana", 8 )); // delete age> 10 = Iterator iterator list.iterator (); the while (iterator.hasNext ()) { the Person Person = (the Person) Iterator.next (); IF (person.age> 10 ) { the Iterator.remove (); } } the System.out .println (List); / * the Lambda achieve * the set of each element into the test method, * if the return value is true, then remove the element, configured Predicate object * / list.removeIf (ELE -> ELE .age <= 10 ); System.out.println (List); } }
7.3 New Thread
/ ** * open a thread * 1. Thread class inheritance * 2.new Thread () Runnable interfaces * / public class Exercise5 { public static void main (String [] args) { Thread T = new new Thread (() -> { for ( int I = 0; I <10; I ++ ) { System.out.println (I); } }); t.start (); } }
8. Java interface built-in functions (we will be able to take over with, do not have to define interfaces)
// the Predicate <T>: parameters: T, Return Value: Boolean // IntPredicate int -> Boolean // LongPredicate Long -> Boolean // DoublePredicate Double -> Boolean // Consumer <T>: parameters: T, return value: void // IntConsumer int -> void // LongConsumer Long -> void // DoubleConsumer Double -> void // Function <T, R & lt>: parameters: T, return value: R & lt // IntFunction <R & lt> int -> R & lt / / LongFunction <R & lt> Long -> R & lt // DoubleFunction <R & lt> Double -> R & lt // IntToLongFunction int -> Double // ... // Supplier <T>: Parameters: None Return Value: T // UnaryOperator <T>: parameters: T, return value: T // BinaryOperator <T>: parameters: T, T, return value: T // BiFunction <T, the U-, R & lt>: parameters: T, U, return value: R & lt // BiPredicate <T, the U->: parameters: T , U, return value: Boolean // BiConsumer <T, the U->: parameters: T, U, return value: void
9. Closure problem?
public class ClosureDemo { public static void main(String[] args) { } private static Supplier<Integer> getNumber(){ int num = 10; return () -> { return num; }; } }
public class ClosureDemo2 { public static void main (String [] args) { // cited in the closure must be constant, when compiled, to a default fianl int A = 101 ; Consumer <Integer> C = ELE -> { System.out.println (ELE); }; c.accept (A); } }