One of the six new features of Java8 functional programming-Lambda expressions

Simply put, the purpose of lamda expression is to replace anonymous inner classes, because anonymous inner classes look bloated and can also be understood as a piece of code that can be passed.

Several characteristics of functional programming:

Closures and high-level functions -closures, treat the function as an object, high-level functions-take the function as a parameter and return a value.

Lazy calculation -When an expression is bound to a variable, calculation is not performed, and calculation is performed only when the evaluation expression requires a value, to achieve the effect of delayed calculation.

No side effects -external variables cannot be modified in the function, and the variables passed into the function must be final. Function programming is a piece of code that can complete a task. If you modify external variables, it may cause some confusion.

The basic syntax of Lambda programming:

(Type1 param1, Type2 param2, ..., TypeN paramN) -> { statment 1;
statment 2; ... return statmentM ;}

1. In most cases, the compiler can infer the parameter type from the context, so the parameter type can be omitted:

(param1, param2, ..., paramN) -> { statment 1;
statment 2; ... return statmentM ;}

 2. When there is only one lambda expression parameter, the parentheses can be omitted:

param1 -> { statment 1;
statment 2; ... return statmentM ;}

3. When the lambda expression contains only one statement, the braces can also be omitted.

param1 ->  statment 1;

Interface when defining 2 functions, one is 2 parameters, one is one parameter:

@FunctionalInterface
public interface LambdaTestInterface {
    void test(String a1, String a2);
}
@FunctionalInterface
public interface LambdaTestInterface1 {
    void test(String a1);
}

Test category:

package allen.java.JDK18;

public class LamdbaTest {
        public static void main(String[] args) {
            //内部类模式
            LambdaTestInterface t1 = new LambdaTestInterface() {
                @Override
                public void test(String a1, String a2) {
                    System.out.println(a1 + " " + a2);
                }
            };
            //标准模式
            LambdaTestInterface t2 = (String a1, String a2) -> {
                System.out.println(a1 + " " + a2);
            };
            //省略参数类型
            LambdaTestInterface t3 = ( a1, a2) -> {
                System.out.println(a1 + " " + a2);
            };
            //省略大括号
            LambdaTestInterface t4 = ( a1, a2) -> System.out.println(a1 + " " + a2);
            //省略小括号和括号
            LambdaTestInterface1 t5 = a1 -> System.out.println(a1);
            
            test1(t1,"hello", "world");
            test1(t2,"hello", "world");
            test1(t3,"hello", "world");
            test1(t4,"hello", "world");
            test2(t5,"hello");
        }

    public static  void test1 (LambdaTestInterface t1,String s1, String s2){
        t1.test(s1 ,s2);
    }
    public static  void test2 (LambdaTestInterface1 t1,String s1){
        t1.test(s1);
    }
}

Results of the:

hello world
hello world
hello world
hello world
hello

Process finished with exit code 0

In the above example, why must a function be defined as an interface, because a function is an interface is a way, and a lamdba expression is a purpose. Only when a function is an interface can a function be an interface.

Why define methods test1 and test2, because the function is an interface and cannot be called directly, but can be used as a parameter.

Note that I need to change the thinking here. For example, I want to find the maximum number in an array. Before, I defined a method and passed in the array. The method returns the maximum number. This is the method.
Now I define an interface function. The interface function is only specified With the input parameter type and return value type of your function, the logic of finding the maximum value in the function can be implemented by yourself and defined as a function. Just pass this function in where needed.

Guess you like

Origin blog.csdn.net/pengweismile/article/details/109692537