This is the fourth day of my participation in Gwen Challenge

Lamdba expressions are a new feature in Java 1.8.

Is an anonymous function that serves as an instance of a functional interface.

Lambda expressions can represent closures

For example,

// An anonymous implementation of the original interface
Comparator<Integer> comparator = new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				returno1.compareTo(o2); }};//Lambda expressions
Comparator<Integer> com2 = (o1,o2) -> o1.compareTo(o2);
Copy the code

grammar

(parameter list) -> single statement or (parameter list) ->{method body; };Copy the code

-> Lambda operator

-> List of Lambda parameters on the left, wrapped in parentheses (parameters for abstract methods in the interface)

  • Optional type declarations: Parameter types can be omitted (type inference)

    (Integer o1,Integer o2) -> { return o1.compareTo(o2); }; 
    // Optional for parameter types (o1, O2)
    Comparator<Integer> com2 = (o1,o2) -> { return o1.compareTo(o2); }; 
    Copy the code
  • Optional parameter parentheses: Only one parameter can be written without parentheses. Zero or more parameters must be written

    // 1. A parameter does not omit curly braces
    (s) -> System.out.println(s);
    // 2. Only one argument can omit the parentheses
    s -> System.out.println(s);
    // 3. If there are no parameters or multiple parameters, do not omit them
    () -> System.out.println("hello");
    Copy the code

-> Lambda body on the right, wrapped in curly braces, and semicolon required at the end (overridden abstract method body)

  • Optional braces: If there is only one statement, the braces can be omitted

    // 1. A statement does not omit braces
    s -> { System.out.println(s); };
    // 2. You can omit braces when there is only one statement
    s -> System.out.println(s);
    //3. Do not omit multiple statements
    s -> {
        System.out.println("hello");
        System.out.println(s);
    };
    Copy the code
  • Optional return keyword: If braces are omitted, the return keyword needs to be removed

    // 1. Do not omit braces
    (a,b) -> { return a+b; };
    // 2. Omit the curly braces. Otherwise, an error will be reported
    (a,b) -> a+b;
    Copy the code

Lambda is essentially an instance of a functional interface ==

An interface that contains only one abstract method is called a functional interface

You can use the FunctionalInterface annotation on the interface to check if it is a FunctionalInterface

So you can give a lambda expression as an argument to a method

Arrays.sort(list, (String s1, String s2) -> (s1.compareTo(s2))); / / equivalent to
Comparator<String> sortByName = (String s1, String s2) -> (s1.compareTo(s2));  
Arrays.sort(list, sortByName);  
Copy the code

Variable scope

1) Outer local variables can be accessed in lambda expressions

You can only access variables that are declared final, but you can implicitly have final semantics without explicitly declaring them

// num
int num = 1;
Converter<Integer, String> s =
        (param) -> String.valueOf(param + num);
Copy the code

However, the local variable num must not be modified by later code (that is, implicitly have final semantics).

int num = 1;  
Converter<Integer, String> s = (param) -> String.valueOf(param + num));
s.convert(2);
num = 5;  
If you enclose this additional scope, Local variable num defined in an enclosing scope must be final or effectively
Copy the code

== Attempting to modify local variables in a Lambda expression is not allowed ==

2) Lambda expressions do not allow declarations of arguments or local variables with the same name as external local variables.

// Local variables
int a;
Comparator<Integer> com3 = (o1,o2) -> {
			int a = 9;
			returna; }; Java: Variable A has been defined in method test2()/ / parameters
inta; Comparator<Integer> com3 = (a,o2) -> o2; Java: Variable A has been defined in method test2()Copy the code

This is the difference between

In lambda this represents the external class

Let’s print his this using an anonymous inner class and a lambda, respectively

// lambda expressions
Comparator<Integer> com1 = (o1,o2) -> {
		System.out.println("lambda:"+thisgetClass());
		return o1.compareTo(o2);
		};
// Anonymous inner class
Comparator<Integer> com2 = new Comparator<Integer>() {
    @Override
   public int compare(Integer o1, Integer o2) {
        System.out.println("Anonymous inner class:"+thisgetClass());
        returno1.compareTo(o2); }};/ / call
com1.compare(1.2);
com2.compare(1.2);
Copy the code

Results:

lambda:class com.xxxx.LambdaDemoAnonymous inner class:class com.xxxx.LambdaDemoThe $1Copy the code

You can see that the output of the lambda expression is LambdaDemo, and the output of the anonymous inner class has a $1 that represents the anonymous class in the LambdaDemo class