one , summary

        Lambda expression --> Functional programming ( Function programming idea )

        Object oriented programming idea :

                It is emphasized that 【 object 】, Something must be done in the form of an object , In general, it will be more complex .

               
for example : Multithreaded execution task , Objects need to be created , Objects need to implement interfaces Runnable, We want to do it ourselves , Need to run Method is passed to the thread object , So much trouble ? Will it be OK to implement it directly soon ?

        Function programming idea :

                Function requires input , Output , Use the input quantity to calculate the output quantity ,【 Do something with something 】

                Is to try to ignore the complex usage of objects --- Emphasize what to do , Not by what ,

                Also perform threaded tasks , Using the idea of functional programming , It can be executed directly by passing a piece of code to the thread object , There is no need to create a task object .

                In a word , The idea of functional programming can complete the amount of code that object-oriented wants to do through a piece of code

two ,Lambda Format of expression

        1. standard format :

                ( parameter list ) -> { code }

        2. Format description :

                - The syntax in the small enclosure is consistent with the parameter list of traditional methods , Leave blank without parameters , Multiple parameters are separated by commas

                -  【->】  Is a newly introduced syntax format , Represents pointing action

                - The syntax in braces is consistent with the requirements of traditional method body

        3. Case description

                First thread case
Thread thread1 = new Thread(new Runnable() { @Override public void run () {
System.out.println(" The task code that the thread needs to execute 1"); } }); thread1.start(); // Lambda expression Thread
t2 = new Thread(()->{ System.out.println(" The task code that the thread needs to execute 2"); }); t2.start();
                The second comparator case
List<Integer> list = new ArrayList<>();
Collections.addAll(list,11,22,33,44,55); System.out.println(" Collection before sorting :" + list);
// Normal writing format of comparator Collections.sort(list, new Comparator<Integer>() { @Override
public int compare (Integer o1, Integer o2) { return o2-o1; } }); // Lambda expression
Collections.sort(list,(Integer o1, Integer o2)->{return o2-o1;});
System.out.println(" Sorted collection :" + list);

three , Expression format parsing

        Lambda Expression format :()->{}

                1. The contents written in the parentheses are consistent with the parameter list of the abstract method in the interface

                2. The content written in braces is consistent with the abstract method body in the implementation interface

                3. The arrow is fixed

four ,Lambda Use condition of expression

        first , All interfaces ;    secondly , There is one and only one interface in the interface , Can only be used lambda expression

        1. Interface with only one abstract method in the interface , It's called a functional interface

        2. If it is a functional interface , Then you can use @FunctionalInterface Annotation mark

  The one above is Thread Bottom layer of thread , The following is Collections Bottom layer of

        reflection ( extend ):

         Collections There is only one abstract method in the interface compareTo(), Why? Java The bottom didn't give it @FunctionalInterface Annotation mark ?( More underlying code , Inconvenient screenshot , don 't worry , I've seen it for you )
        because :lambda Expression belongs to   add a beautiful thing to a contrasting beautiful thing  
Function of , Each class has its own function or role , and Comparable Interface is mainly used to implement comparator methods for user-defined classes ,@FunctionalInterface Annotation marks are mainly used to show the compiler , To provide information to the compiler , With or without this annotation , As long as the requirements of functional formula are met , Then it is a functional interface

five ,Lambda Omitted form of expression

        Lambda It's quite simple , Look down

        Lambda Omitted form of expression :

                1. Formal parameter types in parentheses can be omitted

                2. If there is only one parameter in parentheses , Then the parentheses can be omitted

                3. If there is only one statement in braces , So braces , semicolon ,return Can be together ellipsis

        Code case :

                1. The first is the thread case
new Thread(()-> System.out.println(" ellipsis ")).start();
                That's it , A thread is created , And it can start smoothly .

                If you don't think it's clear enough , Look at the second case

                2. Case of the second comparator
public static void main (String[] args) { // Comparator case ArrayList<Integer> list =
new ArrayList<>(); list.add(11); list.add(22); list.add(33); list.add(44);
System.out.println(" Before sorting :" + list); // Conventional writing method of comparator Collections.sort(list, new
Comparator<Integer>() { @Override public int compare (Integer o1, Integer o2) {
return o2 - o1; } }); // Lambda expression Collections.sort(list,(o1,o2)->o2-o1);
System.out.println(" After sorting :" + list); }
        

six , Expression form

        1. Form of variable : The type of variable is functional interface , You can copy one Lambda expression 【 Not commonly used 】
// Form of variable Runnable r = ()->{ System.out.println(" Task code "); }; // Variable of functional interface type
Thread t = new Thread(r);
        2. Form of parameter : The formal parameter type of the method is a functional interface , You can pass in one Lambda expression 【 Commonly used 】
// Form of variable - comparator Comparator<Integer> comparable = (o1, o2)->{return o2 - o1;}; //
Create collection ArrayList<Integer> list = new ArrayList<>(); // Store data
Collections.addAll(list,11,22,33,44,55); // The functional interface type of parameter types , Pass to Collections
Collections.sort(list,comparable);
        3. Form of return value : The return value type of the method is a functional interface , You can return one Lambda expression 【 Commonly used 】
// Define a method public static Comparator<Integer> getComparator(){ return (Integer
o1,Integer o2)->{return o2-o1;}; } public static void main (String[] args) { //
Return value form Collections.sort(list,getComparator()); }

Technology