JAVA Stream 中 Function<T, R> 中 apply,compose, andThen 方法学习总结

Function 实际作用以及个人理解

在正常使用中,我们平时定义方法都是类似这样

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

然后通过在其他代码中引用改方法来使用,例如:

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

Stream 中的 Function 实际上解决了一个简单的问题,那就是我们在实际书写代码的时候,一个方法一般只能作为代码中的某一行函数,用于处理里面的数值,但是并不能将一个方法作为一个参数,将方法以参数的形式将方法传递到下一个方法中继续使用。

源码以及使用示例

在这里插入图片描述
Function 是作为一种方法接口,两个泛型 T, R 在这里分别指代的是 方法的入参以及返回值的泛型。

1. apply

在这里插入图片描述
apply 的作用为执行当前方法函数,t 为入参,R 为返回值类型
示例

    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);
	}

结果如下:
在这里插入图片描述

2. compose

在这里插入图片描述
compose 看内部实现,这个方法的入参也为 Function 类型,当调用时会先执行 作为入参的 Function 中的apply 方法,再将计算的结果作为原有方法的 apply 入参
示例

    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);
    }

结果如下:
在这里插入图片描述

3. andThen

在这里插入图片描述
andThen 与compose 类似,但是方向相反
示例

    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);

    }

结果如下:
在这里插入图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值