• Java 常用函数式接口 —— Function接口


    JDK提供了大量常用的函数式接口以丰富Lambda的典型使用场景,它们主要在 java.util.function 包中被提供。 下面是最简单的Function接口及使用示例。

    Function接口概述

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

    @FunctionalInterface
    public interface Function<T, R> {
        R apply(T t);
        ......
    }

    抽象方法:apply

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

    import java.util.function.Function;
    
    public class DemoFunctionApply {
    
        public static void main(String[] args) {
            method(s -> Integer.parseInt(s));
        }
        
        private static void method(Function<String, Integer> function) {
            int num = function.apply("10");
            System.out.println(num + 20);
        }
    }

    运行程序,控制台输出:

    30

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

     

    默认方法:andThen

    Function 接口中有一个默认的 andThen 方法,用来进行组合操作。JDK源代码如:

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

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

    import java.util.function.Function;
    
    public class DemoFunctionAndThen {
    
        public static void main(String[] args) {
            method(
                    str -> Integer.parseInt(str)+10,
                    i -> i *= 10
            );
        }
    
        private static void method(Function<String, Integer> one, Function<Integer, Integer> two) {
            int num = one.andThen(two).apply("10");
            System.out.println(num + 20);
        }
    }

    运行程序,控制台输出:

    220

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

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

    练习:自定义函数模型拼接

    题目

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

    String str = "赵丽颖,20";
    1. 将字符串截取数字年龄部分,得到字符串;
    2. 将上一步的字符串转换成为int类型的数字;
    3. 将上一步的int数字累加100,得到结果int数字。

     

    解答

    import java.util.function.Function;
    
    public class DemoFunction {
    
        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);
        }
    }

    运行程序,控制台输出:

    120

              

  • 相关阅读:
    python函数
    python文件IO操作
    LAMP项目上线
    linux下的小工具
    linux下自有服务
    Lesson_Swift2
    枚举
    使用文件流下载附件
    Global中的Timer计时器
    IE11下的NPOI导出提示__doPostBack未定义解决方案
  • 原文地址:https://www.cnblogs.com/liyihua/p/12286100.html
Copyright © 2020-2023  润新知