JAVA中Function的使用

本文详细介绍了Java中Function接口的使用方法,包括参数类型、常用方法及其源码解析,并通过示例展示了如何运用compose和andThen方法进行函数组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


参考: https://blog.csdn.net/boyan_HFUT/article/details/99618833

一、方法介绍

表示接受一个参数并产生结果的函数。

参数类型

  • T - 函数输入的类型
  • R - 函数的结果类型

方法介绍

  • R apply(T t)
    将此函数应用于给定的参数。
  • default Function<V, R> compose(Function<? super V, ? extends T> before)
    返回一个组合函数,首先将before函数应用于其输入,然后将此函数应用于结果。 如果任一函数的评估引发异常,则将其转发给组合函数的调用者。
  • default Function<T, V> andThen(Function<? super R, ? extends V> after)
    返回一个组合函数,首先将此函数应用于其输入,然后将after函数应用于结果。 如果任一函数的评估引发异常,则将其转发给组合函数的调用者。
  • static Function<T, T> identity()
    返回一个总是返回其输入参数的函数。

源码

@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.
     * 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
     * @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
     * @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.
     * 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
     * @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;
    }
}

二、demo

public class Test {
    public static void main(String[] args) throws Exception {
        Function<Integer, Integer> add = p -> p + 10;
        Integer result = add.apply(10);
        // 这里会输出 20,因为这个函数定义的操作时把参数加上 10 后返回
        System.out.println(result);


        Function<Integer, Integer> multiplyTen = a -> a * 10;
        Function<Integer, Integer> addTen = a -> a + 10;
        // 先增加 10,然后再乘 10,输出结果 110
        Function<Integer, Integer> addTenThenMultiplyTen = multiplyTen.compose(addTen);
        System.out.println(addTenThenMultiplyTen.apply(1));


        // 先乘 10,然后再加 10,输出结果 20
        Function<Integer, Integer> multiplyTenAddTenThen = multiplyTen.andThen(addTen);
        System.out.println(multiplyTenAddTenThen.apply(1));
    }


}

结果
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值