Lambda expressions that have puzzled themselves for a long time

preface

  today, let's talk about Lambda, which has puzzled me for a long time. Why do you say that? Because I never took the initiative to learn Lambda before. I didn't think about a lot of code using Lambda expressions. But recently, this Lambda expression appeared a little frequently, which affected my next study, so I went to learn it. I have just finished my study and come to summarize it. It can be regarded as consolidation. Of course, I also hope this article can help more students prepare to learn Lambda.
    in this article, I will use many easy to understand examples to teach you how to use Lambda. I suggest you type the case yourself, so that you will be more impressed!!
ps: all the examples used in this article are   An interface, the following corresponding Lambda implementation and the simplified Lambda implementation

Lambda expression

Instructions for use

   although Lambda expressions can simply implement interfaces, these interfaces must have only one abstract method!!. Here is an annotation for you to explain: @ functional interface is used on the interface (modifying the functional interface). Once this annotation is used, there can only be one abstract method in the interface.

Basic grammar and grammar simplification

(): used to describe the parameter list
{}: used to describe the method body, which can sometimes be omitted
->: lambda operator reads goes to

  • Implementation of parameterless and return type free interface (method curly bracket reduction)
/**
*Functional interface
*/
@FunctionalInterface//Decorate functional interfaces (only one abstract method can be used in an interface)
public interface NoneReturnNoneParmeter {
    void test();
}
--------------------------------------------------------------
//Implement the interface using Lambda expressions:
NoneReturnNoneParmeter lambda1=()->{
            System.out.println("hello word");
        };
--------------------------------------------------------------
//Compact Lambda expression:
/**
* Method brace reduction:
* If there is only one statement in the method body, braces can be omitted
*/
NoneReturnNoneParmeter lambda1=()->System.out.println("hello word");

  • Interface with return value and one parameter (reduction of parentheses and supplement of reduction of braces)

      Scan VX for Java data, front-end, test, python and so on

/**
*Functional interface
*/
@FunctionalInterface
public interface SingleReturnSingleParmeter{
    int test(int a);
}
--------------------------------------------------------------
//Implement the interface using Lambda expressions:

SingleReturnSingleParmeter lambda3=(int a)->{
            return a*2;
        };
--------------------------------------------------------------
//Compact Lambda expression:
/**
* Parameter parentheses
* If there is only one parameter in the parameter list, the parentheses can be omitted
*/
SingleReturnSingleParmeter lambda3=a->{
				return a*2;
			}
--------------------------------------------------------------

/**
* Curly braces concise supplement:
* If the only statement in the method body is a return statement
* return must also be omitted when the braces are omitted
*/
SingleReturnSingleParmeter lambda3 a->a*2;

  • Interface without return value and multiple parameters
/**
*Functional interface
*/
@FunctionalInterface
public interface NoneReturnMutipleParmeter {
    void test(int a,int b);
}
--------------------------------------------------------------
//Implement the interface using Lambda expressions:

NoneReturnMutipleParmeter lambda2=(int a,int b)->{
            System.out.println("hello word");
        };
--------------------------------------------------------------
//Compact Lambda expression:
/**
* Parameter type
* Because the number and type of parameters have been defined in the abstract method of the interface
* Therefore, the type of parameter in Lambda expression can be omitted
* Note: if the type needs to be omitted, the type of each parameter should be omitted
* Never omit one
*/
NoneReturnMutipleParmeter lambda2=(a,b)->
				System.out.println("hello word");

Advanced (Reference method)

/**
*Functional interface
*/
@FunctionalInterface
public interface SingleReturnSingleParmeter{
    int test(int a);
}
/**
*Method reference:
* be careful:
*  1.In the referenced method, the number and type of parameters must be consistent with the methods defined in the interface
*  2.The type of return value must also be consistent with the method in the interface
*/
public class Syntax {
 /**
 * Custom implementation method
 */
 private static int change(int a){
 	return a*2; 
  }
  /**
  *Common reference
  */
 SingleReturnSingleParmeter lambda1=a->a*2;
 
//Simplification: you can quickly point the implementation of a Lambda expression to an implemented method
SingleReturnSingleParmeter lambda4=a->change(a);

/**
* If a method's subordinate is a static method, it belongs to a class, and others are subordinate objects
* Syntax: method's subordinate:: method name
*/
SingleReturnSingleParmeter lambda5=Syntax3::change;
}

  Scan VX for Java data, front-end, test, python and so on

Tags: Java Back-end

Posted on Mon, 06 Dec 2021 14:49:41 -0500 by scuzzo84