Function

发布时间:2023年12月18日

原文链接

1、Function API用法

  • java.util.function.Function:Function 接口有4个方法
    • apply:抽象方法。将此函数应用于给定的参数。参数t通过具体的实现返回R
    • compose:default方法。返回一个复合函数,首先执行fefore函数应用于输入,然后将该函数应用于结果。如果任意一个函数的求值引发异常,则将其传递给组合函数的调用者
    • andThen:default方法。返回一个复合函数,该复合函数首先对其应用此函数它的输入,然后对结果应用after函数。如果任意一个函数的求值引发异常,则将其传递给组合函数的调用者
    • identity:static方法。返回一个始终返回其输入参数的函数。
  • JDK8在线源码英文文档:https://nowjava.com/readcode/jdk8
  • JDK8在线源码中文文档:https://www.matools.com/api/java8

1-1、apply

  • 示例:apply
        Function<String, String> function = new Function<String, String>() {
            @Override
            public String apply(String s) {
                return s+"apply";
            }
        };
        // 123apply
        System.out.println(function.apply("123"));


        // lambda写法
        Function<String, String> function1 = str -> str+"apply";
        System.out.println(function1.apply("123"));

1-2、compose

  • 示例:compose(先执行function2 后执行function1)
        Function<String, String> function1 = s -> "Hello, "+s;
        Function<String, String> function2 = s -> "my name is "+s;
        String result = function1.compose(function2).apply("zhangSan");
        // Hello, my name is zhangSan
        System.out.println(result);

1-3、andThen

  • 示例:andThen(先执行function1 后执行function2)
        Function<String, String> function1 = s -> "my name is "+s;
        Function<String, String> function2 = s -> "Hello, "+s;
        String result = function1.andThen(function2).apply("zhangSan");
        // Hello, my name is zhangSan
        System.out.println(result);

1-4、identity

  • 示例:identity(返回一个总是返回其输入参数的函数)
        Function<String, String> identity = Function.identity();
        String aaa = identity.apply("aaa"); // R apply(T t);
        System.out.println(aaa);

2、源码翻译

  • 下面这个是源码翻译
package java.util.function;

import java.util.Objects;

/**
 * Represents a function that accepts one argument and produces a result.
 * 表示接受一个参数并产生结果的函数
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #apply(Object)}.
 * 这是一个functional interface,它的功能方法是apply(Object) 。
 *
 * @param <T> the type of the input to the function 函数输入的类型
 * @param <R> the type of the result of the function 函数返回的类型
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     * 将此函数应用于给定的参数
     *
     * @param t the function argument 函数参数
     * @return the function result 函数结果
     */
    R apply(T t);

    /**
     * Returns a composed function that first applies the {@code before}
     * function to its input, and then applies this function to the result.
     * 返回一个组合函数,首先将before函数应用于其输入,然后将此函数应用于结果。
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     * 如果任一函数的评估引发异常,则将其转发给组合函数的调用者。
     *
     * @param <V> the type of input to the {@code before} function, and to the
     *           composed function 输入到 before函数的类型,并且组合函数
     * @param before the function to apply before this function is applied 应用此功能之前应用的功能
     * @return a composed function that first applies the {@code before}
     * function and then applies this function 一个组合函数首先应用 before函数,然后应用此功能
     * @throws NullPointerException if before is null
     *
     * @see #andThen(Function)
     */
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    /**
     * Returns a composed function that first applies this function to
     * its input, and then applies the {@code after} function to the result.
     * 返回一个组合函数,首先将此函数应用于其输入,然后将after函数应用于结果。
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     * 如果任一函数的评估引发异常,则将其转发给组合函数的调用者。
     *
     * @param <V> the type of output of the {@code after} function, and of the
     *           composed function
     * @param after the function to apply after this function is applied
     * @return a composed function that first applies this function and then
     * applies the {@code after} function 一个组合函数首先应用此函数,然后应用 after函数
     * @throws NullPointerException if after is null
     *
     * @see #compose(Function)
     */
    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    /**
     * Returns a function that always returns its input argument.
     * 返回一个总是返回其输入参数的函数。
     *
     * @param <T> the type of the input and output objects to the function 函数的输入和输出对象的类型
     * @return a function that always returns its input argument 一个总是返回其输入参数的函数
     */
    static <T> Function<T, T> identity() {
        return t -> t;
    }
}
文章来源:https://blog.csdn.net/ERJI2022/article/details/132753240
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。