Chapter 2 lambda expressions and lambda expressions
I. process anonymous internal classes
1. Runnable interface
1 new Thread(new Runnable() {2 public void run() {3 System.out.println("hello world!!!");4 }5 }).start();6 7 // lambda8 new Thread(()->System.out.println("hello lambda!!!")).start();
Note:
- The above method is the original anonymous internal class method.
- The following method is lambda expression.
Lambda basic syntax:(Parameter list)-> {method body}
Note:
- Parameter List: (String x, String y) Basic Form
- If no parameters exist, use "()".
- If the parameter type can be inferred, use (x, y)
- If it is a single parameter and its type can be inferred, directly use x
- Method body: if there is only one sentence, you can remove "{}", as shown above
2. Comparator Interface
1 List <String> strList = Arrays. asList ("zhaojigang", "nana", "tianya"); 2 // the original method 3 Collections. sort (strList, new Comparator <String> () {4 @ Override 5 public int compare (String s1, String s2) {6 return s1.compareTo (s2); 7} 8 }); 9 10 // lambda11 Collections. sort (strList, (s1, s2)-> s1.compareTo (s2 ));
Note: s1 and s2 are inferred to be of the String type based on the wildcard type of comparator and the generic type of strList. parameter types can be omitted.
Ii. Method reference
1 // lambda2 Collections. sort (strList, (s1, s2)-> s1.compareTo (s2); 3 // reference 4 Collections. sort (strList, String: compareTo); 5 // lambda6 strList. forEach (x-> System. out. println (x); 7 // method reference 8 strList. forEach (System. out: println );
Note:
- The effects of the first two statements are the same, while those of the last two statements are the same. You can compare the reference of the method with the lambda statements to determine which parts are equivalent.
- The foreach method of the Collection class is the enhancement of the enhanced for loop.
Note:
- Method reference syntax
- Object: instance method => equivalent to "lambda expression that provides method parameters"
- Class: static method => equivalent to "lambda expressions that provide method parameters"
- Eg. System. out: println is equivalent to x-> System. out. println (x)
- Class: instance method => the first parameter is the object of the execution method.
- Eg. String: compareTo is equivalent to (s1, s2)-> s1.compareTo (s2)
Iii. Local Variables
The local variables of the lambda operation must be final, that is, the local variables used in the lambda expression (the variables or parameters in the method body) can only be read and cannot be changed.
Note: The reason for listing this constraint is to prevent thread insecurity. There may be doubts that local variables are private methods. How can thread security problems occur?
Explanation: Suppose I started a thread in the method body and used a lambda expression to operate on a local variable count (note that the variable is not declared in lambda, but lambda can be used. This is"Closure"), And in the method body outside the thread, the method also operates the count, then there may be thread security issues.
Iv. interface changes
Java interfaces can also be written to implement default-level instance methods and static methods.
1 public interface LambdaInterface {2 // default method 3 default void defaultMethod () {4 System. out. println ("xxxx"); 5} 6 // static method 7 static void staticMethod () {8 System. out. println ("xxxx"); 9} 10}
1 public class TestInterface implements LambdaInterface {2 public static void main (String [] args) {3 LambdaInterface test = new TestInterface (); 4 test. defaultMethod (); // test the default method 5 6 LambdaInterface. staticMethod (); // static Method Test 7} 8}
Purpose: When you want to add some methods to the old interface, but do not want the old implementation class of the interface to implement these methods, you can use this technique.