# What is a Lamdba expression

According to Baidu:

lambda expression is an anonymous function. lambda expression is based on λ The name of calculus directly corresponds to the lambda abstraction. It is an anonymous function, that is, a function without a function name. Lambda expressions can represent closures (note the difference from the traditional sense of Mathematics).

From the introduction, Lamdba expression is an anonymous function of closure

# Basic syntax of Lambda expressions

```Parameters -> an expression
```

This is the basic representation of lambda. The one before the arrow is the parameter and the one after the arrow is the method expression
If there are multiple statements to be executed in the method expression, use {} to enclose the expression, and then use - > to connect the two ends, which means passing parameters to the method body

```Parameters -> {many expressions} ;
```

Lambda expressions have the following important features

• Optional type declaration: there is no need to declare parameter types, and the compiler can uniformly identify parameter values.
• Optional parameter parentheses: one parameter does not need to define parentheses, but multiple parameters need to define parentheses.
• Optional curly braces: if the body contains a statement, curly braces are not required.
• Optional return keyword: if the body has only one expression return value, the compiler will automatically return the value. Braces need to specify that the expression returns a value.

## Specific use of Lambda expression in code

### Parameterless Lamdba expression

First, we create an interface that contains a method without parameters

```public interface Speaker {
void speak();
}
```

Next, we use Lambda to implement the function of this method. The invoke method is used to execute the methods in the anonymous inner class

```public class Main {
public static void main(String[] args) {
invoke(() -> {
System.out.println("asd");
});
}
public static void invoke(Speaker speaker){
speaker.speak();
}
}
```

The printout is as follows

```asd
```

### Lambda expression with parameters

Here, we use the stream stream to filter the list, where e is the parameter we passed in.

```        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
List<Integer> collect = list.stream().filter(e -> e < 5).collect(Collectors.toList());
collect.forEach(e-> System.out.println(e));
```

# Functional interface

Functional interface is an interface newly added in JDK 1.8. The function of this interface is that the class implementing functional interface can be implicitly converted into lambda expression

Create Man interface and implement @ FunctionInterface

```@FunctionalInterface
interface Man{
void speak(String str);
}
```

Use lambda to create an instance that implements the Man interface

```        Man man = str -> System.out.println(str);
man.speak("Hello");
```

In the source code of the functional interface, it is specially explained that there can only be one abstract method in the functional interface. Conceptually, a functional interface has exactly one abstract method, For example, if there are multiple types of methods in Object, there is also one exception:

```@FunctionalInterface
interface Man{
void speak(String str);
boolean equals(Object man);
}
```

However, other methods cannot be implemented by lambda expressions, because they have been implemented by other classes conceptually, a functional interface has exactly one abstract method Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java. lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java. lang.Object or elsewhere.

In addition, methods in functional interfaces can be implemented by default, that is, adding the default keyword before the method name

It should be noted that the default method is not included in the method of functional interface, so you should implement a method in addition to the default method

```@FunctionalInterface
interface Man{
default void defaultspeak(String str) {
System.out.println("I will speak:"+"\""+str+"\"");
}
void speak(String str);
}
```

Call default method

```        Man man = str -> System.out.println(str);
man.defaultspeak("hello");
man.speak("Hello");
```

# Method reference (:: symbol)

In addition to using Lambda expressions and functional interfaces to implement methods for the implementation class of an interface, we can also pass existing methods to the interface through the methods referenced by the methods to create entity classes

For example, we create a number comparison class, in which one method is to compare two numbers and return the maximum value.

```public class NumberComparetor {
public static int compareMax(int i , int j){
return i>j?i:j;
}
}
```

Customize a comparator

```public interface MyComparetor<T> {
Object compare(int x, int y);
}
```

We use method reference to implement the mycomparator interface, pass numbercomparator to it, and then compare the size of the two numbers and output them

```    public void test(){
MyComparetor myComparetor = NumberComparetor::compareMax;
System.out.println(myComparetor.compare(1,2));
System.out.println();
}
```

The output result is

```2
```

Keywords: Java Back-end

Added by aspguy on Wed, 16 Feb 2022 16:20:20 +0200