Learn lambda expressions from 0 to 0.1 (Java Edition)

After several years of coding, there is one thing that seems to be inescapable all the time, and that is lambda expressions.
Whether it is c#, Python or Java, the idea of ​​lambda is common. But the syntax and examples below are java.

Now let's talk about this seemingly difficult lambda expression

What are lambda expressions?

Lambda expression is essentially an anonymous function
So what is an anonymous function?
An anonymous function is a function without a name that can be directly embedded in a method. It only includes function parameter (parameter) + expression (expression) or function parameter (parameter) + statement (statements)
PS: expression produces value eg. a+b;2. Both of these are expressions. Statements focus on operations such as if a==b then c=a; c=a+b assign xx to xx, or loop/judgment
In fact, statements also contain expressions,
so we can treat lambda expressions as A function that outputs a value to use (assign a lambda expression to a value)

The syntax is as follows

// no parameter
() -> expression
// one or multi parameters
parameter -> expression
(param1,param2) -> expression
// muiti statements
(param1,param2) -> {
    
     statements }

For example (JAVA)

(int x, int y) -> x * y;

What problem do lambda expressions solve?

I stole a picture from Big Brother Zhihu. This is java grammar (java 8 and above). You
insert image description here
can see that the process of the whole function is simplified in the picture. From the previous nested code to one line of concise code.

Advanced - for use with the interface

public class Test
{
    
    
    // 接口FuncInter1中定义了有且仅有一个抽象函数
    interface FuncInter1
    {
    
    
        int operation(int a, int b);
    }
    public static void main(String args[])
    {
    
    
		// 使用lambda表达式的方式实现了FuncInter1接口中的唯一函数
        FuncInter1 add = (int x, int y) -> x + y;

        FuncInter1 multiply = (int x, int y) -> x * y;
        // 使用正常的方式调用接口中的参数即可
        System.out.println("Addition is " + add.operation(2, 3));
        System.out.println("Multiplication is " + multiply.operation(2, 3));
    }
}

The current way is to use the lambda expression to implement the interface, and then use the corresponding function of the corresponding interface to call the lambda expression to get the result. Is there an easier way? really have!

public class Test
{
    
    
    // 依然是原来的接口
    interface FuncInter1
    {
    
    
        int operation(int a, int b);
    }
  	// 将接口中的方法封装起来,这样只需要实现方法及输入参数就可以调用了
    private int operate(int a, int b, FuncInter1 fobj)
    {
    
    
        return fobj.operation(a, b);
    }
  
    public static void main(String args[])
    {
    
    
        FuncInter1 add = (int x, int y) -> x + y;
        FuncInter1 multiply = (int x, int y) -> x * y;

		// 直接掉用封好的函数
        System.out.println("Addition is " + operate(6, 3, add));
        System.out.println("Multiplication is " + operate(6, 3, multiply));

    }
}

reference

  1. What statement and expression are introduced: https://farside.ph.utexas.edu/teaching/329/lectures/node11.html
  2. A brief introduction to Java8-lambda expressions: https://www.geeksforgeeks.org/lambda-expressions-java-8/
  3. Image source: https://www.zhihu.com/question/20125256/answer/324121308

Guess you like

Origin blog.csdn.net/ptyp222/article/details/115356004