• 【无标题】Java 函数式接口


    一、目的

          最近项目上使用了很多Function类型的函数形参,同是使用了泛型,所以这里进行一下总结

    二、介绍

           Function 函数就是接受一个参数然后对这个参数进行处理,比如 Function 这种

    三、使用

         3.1 现有主要函数

    函数名称类型
    Function接收一个参数并返回结果的函数
    BiFunction接收两个参数并返回结果的函数
    DoubleFunction接收一个double的参数并返回结果的函数

    一般我们经常使用的是Function 其对应的接口方法为

    接口方法方法描述
    R apply(T t)将此参数应用到函数中
    Function andThen(Function after)返回一个组合函数,该函数结果应用到after函数中
    Function compose(Function before) 返回一个组合函数,首先将入参应用到before 函数,然后再将before函数结果应用到该函数中

    3.2 代码实现:

       3.2.1 apply函数

    1. /**
    2. * 调用函数
    3. * @param aimString 目标参数
    4. * @param firstFunction 第一个处理函数
    5. */
    6. public void andApplyFunction(String aimString, Function firstFunction) {
    7. Integer aimResult = firstFunction.apply(aimString);
    8. System.out.println("最后的结果为----"+ aimResult);
    9. }

      

    调用: 

            andApplyFunction("12", aimString -> Integer.parseInt(aimString + 12));
    

            上面代码就是说明, 我将参数类型为String,值为12的形参对象传递进函数,而firstFunction 定义表示我接收一个String 类型,返回一个Integer 型,对应的处理逻辑是将aimString 加上12 后转为Integer 返回。

    或者

    andApplyFunction("12",Integer::parseInt);

    上面的代码说明Function 是一个Integer::partInt  (是一个lamda表达式,即 paramString -> Integer.parseInt(paramString))

    3.1.2 andThen 函数

       andThen 函数就是将调用者返回的结果传递给下一个函数

    1. /**
    2. * 调用对应的andThen方法
    3. *
    4. * @param firstFunction 第一个处理函数
    5. * @param secondFunction 第二个处理函数
    6. */
    7. public void andThenTest(String aimString, Function firstFunction, Function secondFunction) {
    8. String aimResult = firstFunction.andThen(secondFunction).apply(aimString);
    9. System.out.println("最后的结果");
    10. }

       调用方式:

            andThenTest("12", (s -> Integer.parseInt(s)), s -> s + "need");
    

        上面代码,传递的参数是值为12的字符串,然后对应的第一个参数为 s->Integer.parseInt(s), 第二个参数为接收  Integer类型的参数,然后将这个参数加上 “need" 后返回。

     3.1.3  andThenCompose

         compose 函数就是说明先将参数应用与第二个函数中,然后再将处理后的结果应用于第一个函数中。

    1. /**
    2. * 调用的对应 compose 函数 其中compose 表示 先执行after 函数,然后再执行first 函数
    3. * @param aimString 处理的参数
    4. * @param firstFunction 第一个处理函数
    5. * @param secondFunction 第二个处理函数
    6. */
    7. public void andCompostTest(Integer aimString, Function firstFunction, Function secondFunction) {
    8. Integer aimResult = firstFunction.compose(secondFunction).apply(aimString);
    9. System.out.println("最后的结果为---" + aimResult);
    10. }
            andCompostTest(12, Integer::parseInt, aimString -> 12 + "12");
    

    这里就是将参数为12的形参先加上"12" ,然后将结果返回到第一个函数中,最后调用Integer::parseInt 处理 进行返回。

     3.1.4 泛型的运用

    上面的方法传入的参数是已经被定义好的,但是在实际运用中,这样复用程度低。所以就需要运用泛型进行统一处理形式的定义。

    1. public static U convertTemplate(String aimString, Function function) {
    2. U apply = function.apply(aimString);
    3. System.out.println("产生的结果为---" + apply);
    4. return apply;
    5. }
            convertTemplate("12" , Integer::parseInt);

       上面的代码中,定义了一个泛型 U 然后要求传入的的结果为String,转化后的结果为U。返回后的结果类型也为U.

      这样一来的话我们就可以将对应的入参String 转化成多种结果了。(上面是转化成了Integer)

      或者也可以这样调用

            convertTemplate("12",  s-> s.replace("_",""));

      或者

            convertTemplate("true",Boolean::valueOf);

     你甚至可以使用两个泛型来定义Function 函数形参

    1. public static T convertTemplate(U aimParam, Function function) {
    2. T t = function.apply(aimParam);
    3. return t;
    4. }
    1. convertTemplate("123", Integer::parseInt);

    四、总结

          Function函数定义基本上是规定了一个入参和出参。传入的对参数的处理逻辑。然后要么直接apply ,要么还需要再处理可以用andThen、andCompose 进行二次处理。   可以定义泛型这样就可以不用规定出参和入参的形式了。

  • 相关阅读:
    Python数据分析--Numpy常用函数介绍(3)
    时序预测 | MATLAB实现ARMA自回归移动平均模型时间序列预测
    Android11 实现有线网络和wifi共存
    mac vscode xdebug 调试moodle
    闭包及闭包的使用
    远程互动会议平台是什么?
    C++:初识类与this指针
    MySQL用户管理(创建、修改、删除用户)
    【微软技术栈】C#.NET 中的本地化
    智能家居—ESP32开发环境搭建
  • 原文地址:https://blog.csdn.net/qq_38345598/article/details/133961940