Enjoy learning class guest author: Lao Gu

Reprint please state the source!

preface

Last time we introduced the origin of Lambda, today we will look at the use of Lambda, how to concise

Lambda expression syntax

(parameters)  -> expression
Copy the code

or

(parameters)  ->  { statements;  }
Copy the code

A Lambda expression consists of three parts:

Paramaters: a list of parameters similar to methods, where the parameters are those of a functional interface. The parameter types can be declared explicitly or implicitly inferred by the JVM without being declared, and the parentheses can be omitted if there is only one inferred type.

Be used for; be used for; be used for

3. Method body: can be an expression or a code block, is the implementation of a method in a functional interface. A code block, which can return a value or nothing, is equivalent to the body of a method. If it is an expression, it can return a value or nothing at all.

case

Lambda expressions are essentially anonymous methods such as

public  int add(int x,int y){
	return x + y;
}
Copy the code

Lambda expressions can be written as Lambda

(int x,int y)  ->  {returnx + y; }Copy the code

The parameters of the function method are on the left, and the implementation body is on the right. To be even more succinct, the parameter types can be left out and Java will infer from the context

(x, y)  ->  {returnx + y; }Copy the code

If the implementation body is a statement, the return can also be omitted, along with the curly braces.

(x, y)  -> x + y;
Copy the code

Several scenarios

There are several scenarios for the abstract methods of interfaces, let’s look at them

1. No parameter, no return value

public  void  print(){
	System.out.println("Hello Lambda!");
}
Copy the code

Lambda expression notation

()  ->  System.out.println("Hello Lambda!");
Copy the code

The typical example is the run method in Runnable

public  interface  Runnable  {
	public  abstract  void run();
}
Copy the code

The way I wrote it before

Runnable r1 =  ()  ->  System.out.println("Hello Lambda!");
r1.run();
Copy the code

2, 1 parameter, no return value

public  void  print(String str){
	System.out.println("Hello "  + str);
}
Copy the code

The Lambda method

(str)  ->  System.out.println("Hello "  + str);
Copy the code

If it is a single argument, you can remove the parentheses

str ->  System.out.println("Hello "  + str);
Copy the code

3. Multiple parameters, return values, and multiple statements

public  int add(int x,int y){
	System.out.println("Add!");
	return x+y;
}
Copy the code

The Lambda method

(x,y)  ->  {
	System.out.println("Add!");
	return x+y;
}
Copy the code

Because this is multiple statements, curly braces must be used.

4, multiple arguments, return value, and one statement, omit curly braces and return

public  int add(int x,int y){
	return x + y;
}
Copy the code

Lambda expressions can be written as Lambda

(x, y)  -> x + y;
Copy the code

formula

Top line: left and right in a parenthesis save

On the left is a parameter, so you can omit the parentheses; On the right is a statement that dispenses with curly braces

Next link: left inferred type save

The parameter types on the left can be omitted, with JAVA inferring from context

Hengpi: can province province

Can eliminate as much as possible to make the code concise

Functional interface

Let’s see what a functional interface is:

If an interface with only one abstract method is called a functional interface, you can use the @functionlInterface annotation to identify it and check whether it is a functional interface

Let’s define an operation interface

@functionalInterface // public interface MyFunction {public Integer getValue(Integer num); }Copy the code

Note that only one abstract method is required to implement the generic function implementation

public  Integer operation(Integer num,MyFunction mf){
	return mf.getValue(num);
}
Copy the code

test

Where is Lambda used

In fact, the above knows where lambda is used, is used with functional interfaces. A lambda expression must have the same argument type as the abstract function description of a functional interface, its return type must be compatible with the abstract function’s return type, and it can throw exceptions only within the scope of the function description. A custom function interface, F is the parameter, T is the return value

@FunctionalInterface
interface  Converter<F, T>{
	T convert(F from);
}
Copy the code

Traditional anonymous class approach

Be sure to observe the above parameter types and return types, as they are defined using a stereotype.

Lambda expression scheme

Both implementations above are equivalent. Let’s take a look at common thread examples

new  Thread(()  ->  System.out.println("hello lambda")).start();
Copy the code

It’s pretty neat. This is the beautiful embodiment of Lambda

conclusion

Lambda expressions are good for you. Of course, we need to write them often to master them. Lambda also has some advanced uses, which I will introduce to you next time. Thank you!!

Follow me and share more technical dry goods