An article that makes you feel no more difficult about Lamdba expressions

Xiaobian found that in leetcode (force buckle), many big guys use a lot of lamda expressions in their problem-solving methods, which makes Xiaobian yearn. However, code reading is not very friendly to Xiaobai like Xiaobian, so Xiaobian decided to learn lamdba expressions to better understand and use lamdba. Xiaobian is also Xiaobai, and it is highly recommended to study with Xiaobai.

catalogue

Lamdba expression

  Conclusion: () - > instead of the method in the inner class, {method body} writes the method body.

Use of lamdba expressions

No parameter, no return value

Conclusion:

What's wrong with the following code analysis?

A parameter has no return value

Conclusion:

Multiple parameters have no return value

​ 

Conclusion:

There are parameters and return values

Conclusion:  

Lamdba expression

New after JDK8.0, Lamdba is an anonymous function.

Function: replace some special anonymous inner classes to make programming more concise (special anonymous inner class: the inner class with only one abstract class method in the interface implemented by the anonymous inner class)

lamdba operator: - >

lamdba expression syntax: (with parameters plus parameters) - > {expression;} (when there is only one expression, {} can be omitted)


public interface LamdbaTest {
    public  void  method();
}

class MyClass implements LamdbaTest{
    public  void method(){
        System.out.println("______method()______");
    }

    public static void main(String[] args) {
        //1. Define implementation classes, create objects and use
        LamdbaTest lamdbaTest = new MyClass();
        lamdbaTest.method();

        //2. Anonymous inner class
        LamdbaTest lamdbaTest1 = new MyClass(){
            @Override
            public void method() {
                System.out.println("This is an anonymous inner class!");
            }

        };
        lamdbaTest1.method();

        //lamdba expression
//        Lamdbatest lamdbatest2 = () - > {system. Out. Println ("this is a lamdba expression!");};
        //Make lamdba more concise when there is only one statement
        LamdbaTest lamdbaTest2 = ()-> System.out.println("This is lamdba expression!");
        lamdbaTest2.method();
    }
}

  Conclusion: () - > instead of the method in the inner class, {method body} writes the method body.

Use of lamdba expressions

No parameter, no return value

public interface LamdbaTest {
    //    No parameter, no return value
    public  void  method();
}

class MyClass implements LamdbaTest{
    public  void method(){
        System.out.println("______method()______");
    }

    public static void main(String[] args) {


        //lamdba expression
        // Lamdbatest lamdbatest2 = () - > {system. Out. Println ("this is a lamdba expression without parameters and return!");};
        //Make lamdba more concise when there is only one statement
        LamdbaTest lamdbaTest2 = ()-> System.out.println("This is parameterless and returns nothing lamdba expression!");
        lamdbaTest2.method();
         //When there are multiple statements, no parameters and no return
        LamdbaTest lamdbaTest = ()-> {System.out.println("This is a number of parameterless returns lamdba expression!");
            System.out.println("Don't forget to add{}And;Number Oh");};
        lamdbaTest.method();

    }
}

 

Conclusion:

        1. When only one expression is {} can be omitted, and when there are multiple expressions, {} cannot be omitted, it should be added;.

         2. The lamdba expression can only refer to the outer local variable marked final. The local variable of the lamdba expression should not declare final, but the variable cannot be modified by the following code.

What's wrong with the following code analysis?

package com.luo_sf.map;

public interface LamdbaTest {
    //    No parameter, no return value
    public  void  method();
}

class MyClass implements LamdbaTest{
    public  void method(){
        System.out.println("______method()______");
    }

    public static void main(String[] args) {
        int sum = 5;//Hidden final
        //When there are multiple statements, no parameters and no return
        LamdbaTest lamdbaTest = ()-> {System.out.println("This is a number of parameterless returns lamdba expression!");
            System.out.println("Don't forget to add{}And;Number Oh");
            //sum++;
            System.out.println(sum);};//You can access sum
        lamdbaTest.method();

    }
}

 

You can access the local variable in the lamdba expression, but you cannot modify it, because the variable under the lamdba expression is decorated with hidden final.

A parameter has no return value


public interface LamdbaTest {
    //    A parameter has no return value
    public  void  method(int sum);
}

class MyClass implements LamdbaTest{
    public  void method(int sum){
        System.out.println("______method()______");
    }

    public static void main(String[] args) {
        //When a statement, a parameter is not returned
        LamdbaTest lamdbaTest = (int num)->{
            System.out.println(num+100);
        };
        lamdbaTest.method(100);

        //When multiple statements, a parameter is not returned
        LamdbaTest lamdbaTest1 = (n)-> {System.out.println("This is more than one parameter with no return lamdba expression!");
            System.out.println("Don't forget to add{}And;Number Oh");
            //sum++;
            System.out.println(n+50);};
        lamdbaTest1.method(100);

        //Method 2: when there are multiple statements, one parameter () can be omitted
        LamdbaTest lamdbaTest2 = n-> {System.out.println("This is more than one parameter with no return lamdba expression!");
            System.out.println("Don't forget to add{}And;Number Oh");
            //sum++;
            System.out.println(n+50);};
        lamdbaTest1.method(100);
        

    }
}

 

Conclusion:

1. If there is only one statement in the method body, {} can be omitted. If there are multiple expressions, {} can not be omitted, but must be added;.

2. The parameter class can be omitted and the parameter name can be arbitrary.

3. When the parameter is one, the parameter outside () can be omitted.

Multiple parameters have no return value


public interface LamdbaTest {
    //    Multi parameter parameter has no return value
    public  void  method(int sum , String name);
}

class MyClass implements LamdbaTest{
    public  void method(int sum , String name){
        System.out.println("______method()______");
    }

    public static void main(String[] args) {
        //When a statement, multiple parameters are not returned
        LamdbaTest lamdbaTest = (int num ,String name)->{
            System.out.println(num+100+" I am: "+name);
        };
        lamdbaTest.method(100,"Outlaw maniac");

        //When multiple statements, multiple parameters are not returned
        LamdbaTest lamdbaTest1 = (n,name)-> {System.out.println("This is more than one parameter with no return lamdba expression!");
            System.out.println("Don't forget to add{}And;Number Oh");
            //sum++;
            System.out.println(n+50);
            System.out.println(name);};
        lamdbaTest1.method(100 , "Zhang San");



    }
}

 

Conclusion:

         1. If there is only one statement in the method body, {} can be omitted. If there are multiple expressions, {} can not be omitted, but must be added;.

         2. The parameter class can be omitted, and the parameter name is arbitrary (to be omitted together!).

         3. When there are multiple parameters, the parameter outside () cannot be omitted.

There are parameters and return values


public interface LamdbaTest {
    //    There are parameters and return values
    public  int  method(int sum);
}

class MyClass implements LamdbaTest{
    public  int method( int sum){
       return 1;
    }

    public static void main(String[] args) {
        //lamdba expression
        //Make lamdba more concise when there is only one statement
        LamdbaTest lamdbaTest = (n) -> {return n+100;};
        //The return value needs to be printed
        System.out.println(lamdbaTest.method(100));

        //When there is only one statement, return can be omitted
        LamdbaTest lamdbaTest1 = n ->  n+100;
        //The return value needs to be printed
        System.out.println(lamdbaTest1.method(100));
        //When there are multiple statements, there are parameters and returns
        LamdbaTest lamdbaTest2 = (n)-> {System.out.println("This is a multiple return with parameters lamdba expression!");
            System.out.println("Don't forget to add{}And;Number Oh");
        return n+100;};
        System.out.println( lamdbaTest2.method(100));
      

    }
}

Conclusion:  

         1. If the method body has only one statement, {} and return can be omitted,

         2,   When there are multiple expressions, {} cannot be omitted, and you need to add;.

        3. The parameter class can be omitted, and the parameter name is arbitrary (to be omitted together!).

        4. When there are multiple parameters, the parameter outside () cannot be omitted.

I think it's good. Remember to pay attention to Xiaobian. Welcome comments and praise collection!

Tags: Java Back-end

Posted on Tue, 30 Nov 2021 11:58:17 -0500 by infini