On lambda expression -- suggestions collection

         lambda expressions are not recommended to be used frequently. If they are used frequently, your code will be very concise but difficult to understand, and it will be difficult to maintain later. Colleagues want to beat people after reading the series.


Lambda is a new operator "->" introduced in Java 8, which becomes an arrow operator or lambda operator.

Left: list of parameters for lambda expression.

Right: the function to be performed in the lambda expression, that is, the lambda body.

Depending on the functional interface, lambda expression is the implementation of the interface.


1.lambda makes sentence patterns more concise.

If you don't talk much, say chestnuts:

    void contextLoads() {
        //lambda expression not used
        Runnable runnable1 = new Runnable() {
            public void run() {
        //Using lambda expressions
        Runnable runnable2=()->System.out.println("404");

You can clearly see the difference between using lambda expressions and not using them.

2. Variables in lambda


       int s=5;
        Runnable runnable2=()->System.out.println(s++);

This sentence will report an error because when calling a local variable with lambda, it will be automatically decorated with final

Although constants cannot be changed, we can define numbers and change the values in the array

  int[] ss=new int [1];
        Runnable runnable2=()->{
            System.out.println( );

So you don't report mistakes

  3. There is a parameter value

Consumer<String> consumer = (x)->{System.out.println(x);};

This code takes the parameter obtained by accept as x and then outputs it.

4. There is only one parameter, omitting parentheses

Consumer<String> consumer = x->{System.out.println(x);};

When there is only one parameter, () can be omitted, and the purpose of lambda expression is to save.

5. There are more than two parameters, return values and multiple statements

Comparator<Integer> consumer1 = (x, y)->{
      return Integer.compare(x,y);

The parentheses of the parameter cannot be omitted. return is required.

6. When there is only one statement in lambda

 Comparator<Integer> consumer2 = (x, y)-> Integer.compare(x,y);

Both {} and return can be omitted

Four built-in functions:

lambda expressions rely on four common built-in functions.

typeclassAbstract method
Consumer interfaceConsumer<T>void accept(T t)
Supply type interfaceSupplier<T>T get();
Functional interfaceFunction<T,R>R apply(T t)
Assertive interfacePredicate<T>boolean test(T t)

Custom function interface:

1. Digital type

  public static  List<Integer> Number (int n, Supplier<Integer> s){
        List<Integer> list =new ArrayList<>();
        for(int i=0;i<n;i++){
        return list;
    public static void main(String[] args) {
        List<Integer> num = Number(4,()->(int)(Math.random()*100));

         The above is the method we defined. The first parameter n is the number of numbers we need to add, and the second parameter is a supply function interface.

(how to generate quickly)   List<Integer> list =new ArrayList<>();

Portal idea automatically completes the code and generates a new List in one second. It is more convenient and fast to write code! Non liver blog - CSDN blog)

2. String type

  public static String Str(String str, Function<String ,String>fun){
        return fun.apply(str);
    public static void main(String[] args) {
        String re  = Str("404+",(x)->x+"505");


        The first parameter is a string, and the second parameter is a functional interface. When calling, we assign x + "string" to re, and then output it.

Method reference:

1. Object:: instantiation method name

    public void test1(){
        Consumer<String> con = System.out::println;

2. Class:: static method name

    public void test2(){
        Comparator<Integer> com1 =(x,y)->Integer.compare(x,y);
        Comparator<Integer> com2 = Integer::compare;

The two methods are equivalent. The second method is equivalent to directly calling the static method compare in Integer.

3. Class: instance method name

    public void test3(){
        BiPredicate<String,String> com1 =(x, y)->x.equals(y);
        BiPredicate<String,String> com2 = String::equals;

The two methods are equivalent, but in the second method, the first parameter is the caller of the method and the second parameter is the parameter of the method.

Tags: Java

Posted on Fri, 08 Oct 2021 04:25:45 -0400 by jabapyth