常用函数式接口-Function

本文深入探讨了Java中的Function接口,它用于根据输入类型T转换为输出类型R。文章通过实例展示了如何使用apply方法进行数据转换,并介绍了默认方法andThen进行函数组合。示例代码解释了如何将字符串转换为整数并进行后续操作。此外,还提供了一个自定义函数模型拼接的案例,演示了如何从包含姓名和年龄的字符串中提取并处理年龄。
摘要由CSDN通过智能技术生成

java.util.function.Function<T,R> 接口用来根据一个类型的数据得到另一个类型的数据,前者称为前置条件,后者称为后置条件。

@FunctionalInterface
public interface Function<T, R> {

    R apply(T t);

    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

抽象方法:apply

Function 接口中最主要的抽象方法为: R apply(T t) ,根据类型T的参数获取类型R的结果。 使用的场景例如:将 String 类型转换为 Integer 类型。

public class Demo {
    private static void method(Function<String, Integer> function) {
        int num = function.apply("10");
        System.out.println(num + 20);
    }

    public static void main(String[] args) {
        method(s -> Integer.parseInt(s));
    }
}

当然,最好是通过方法引用的写法。

默认方法:andThen

Function 接口中有一个默认的 andThen 方法,用来进行组合操作。

该方法同样用于“先做什么,再做什么”的场景,和 Consumer 中的 andThen 差不多:

public class Demo {
    private static void method(Function<String, Integer> one,
                               Function<Integer, Integer> two) {
        int num = one.andThen(two).apply("10");
        System.out.println(num + 20);
    }

    public static void main(String[] args) {
        method(str -> Integer.parseInt(str) + 10, i -> i *= 10);
    }
}

第一个操作是将字符串解析成为int数字,第二个操作是乘以10。两个操作通过 andThen 按照前后顺序组合到了一 起。

请注意,Function的前置条件泛型和后置条件泛型可以相同。

自定义函数模型拼接

请使用 Function 进行函数模型的拼接,按照顺序需要执行的多个函数操作为:

  • String str = "张二三,20"
  • 将字符串截取数字年龄部分,得到字符串;
  • 将上一步的字符串转换成为int类型的数字;
  • 将上一步的int数字累加10,得到结果int数字。
public class Demo {
    public static void main(String[] args) {
        String str = "张二三,20";
        int age = getAgeNum(str, s -> s.split(",")[1],
                s -> Integer.parseInt(s),
                n -> n += 100);
        System.out.println(age);
    }

    private static int getAgeNum(String str, Function<String, String> one,
                                 Function<String, Integer> two,
                                 Function<Integer, Integer> three) {
        return one.andThen(two).andThen(three).apply(str);
    }
}

有帮到你的点赞、收藏一下吧

                                                                          需要更多教程,微信扫码即可

                                                                                 

                                                                                         👆👆👆

                                                        别忘了扫码领资料哦【高清Java学习路线图】

                                                                     和【全套学习视频及配套资料】
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值