Summary of apply, compose, andThen methods in Function<T, R> in JAVA Stream

The actual role of Function and personal understanding

In normal use, we usually define methods like this

    public static String fixStringLength(String numberArg){
    
    
        int length = numberArg.length();
        switch (length){
    
    
            case 1:
                return "00"+numberArg;
            default:
                return "fix" + numberArg;
        }
    }

Then use it by referencing the method in other code, for example:

    public static void main(String[] args) {
    
    
        fixStringLength("486");
    }

The Function in Stream actually solves a simple problem, that is, when we actually write the code, a method can generally only be used as a function of a certain line in the code to process the value inside, but a method cannot be used as a A parameter, the method is passed to the next method in the form of a parameter for continued use.

Source code and usage examples

insert image description here
Function is used as a method interface, and the two generic types T and R here refer to the generic types of the method's input parameters and return values.

1. apply

insert image description here
The function of apply is to execute the current method function, t is the input parameter, and R is the return value type
Example :

    public static void main(String[] args) {
    
    
// 定义函数 但没有创建实际方法而是类似于将方法 这里的 function1 作为一个变量,对象  以此来直接在行内引用方法对象 达到和方法一样的效果
        Function<Integer, String> function1 = item -> {
    
    
            item *=  100;
            return item + "";
        };

        String apply = function1.apply(13);
        System.out.println("apply = " + apply);
	}

The result is as follows:
insert image description here

2. compose

insert image description here
Compose looks at the internal implementation. The input parameter of this method is also of the Function type. When calling, it will first execute the apply method in the Function as the input parameter, and then use the calculated result as the apply input parameter of the original method.
Example :

    public static void main(String[] args) {
    
    

        // 定义函数 但没有创建实际方法而是类似于将方法 这里的 function1 作为一个变量,对象  以此来直接在行内引用方法对象 达到和方法一样的效果
        Function<Integer, String> function1 = item -> {
    
    
            item *=  100;
            return item + "";
        };

        String apply = function1.apply(13);
        System.out.println("apply = " + apply);

        // 定义的第二个方法函数
        Function<Integer, Integer> function2 = item -> item + 10;
        // 先将 13 作为function2的入参 然后将结果作为function1的入参继续计算
        String apply2 = function1.compose(function2).apply(13);
        System.out.println("apply2 = " + apply2);
    }

The result is as follows:
insert image description here

3. andThen

insert image description here
andThen is similar to compose, but in the opposite direction
Example :

    public static void main(String[] args) {
    
    

        // 定义函数 但没有创建实际方法而是类似于将方法 这里的 function1 作为一个变量,对象  以此来直接在行内引用方法对象 达到和方法一样的效果
        Function<Integer, String> function1 = item -> {
    
    
            item *=  100;
            return item + "";
        };

        String apply = function1.apply(13);
        System.out.println("apply = " + apply);

        // 定义的第二个方法函数
        Function<Integer, Integer> function2 = item -> item + 10;
        // 先将 13 作为function2的入参 然后将结果作为function1的入参继续计算
        String apply2 = function1.compose(function2).apply(13);
        System.out.println("apply2 = " + apply2);

        // 定义的第三个方法函数
        Function<String, Integer> function3 = item -> Integer.valueOf(item) + 31;
        // 先将 13 作为function1的入参 然后将结果作为function3的入参继续计算
        Integer apply3 = function1.andThen(function3).apply(13);
        System.out.println("apply3 = " + apply3);

        // 使用其他已定义的方法 示例
        String apply4 = function1.andThen(App::fixStringLength).apply(13);
        System.out.println("apply4 = " + apply4);

    }

The result is as follows:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44131922/article/details/131062828