Java内置函数 Function函数

Java内置Function参数,类包是在 java.base 模块下 java.util.function 包中,其方法主要用于对一个请求参数的处理,并返回一个结果。

Function源码

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

Function主要方法

apply方法

当前方法就是我们使用匿名函数时需要重写的方法,其中请求参数和返回参数都需要在我们生成Function对象的时候传进去,而apply方法也是这个类最核心的方法。

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);

创建 简单的Function对象

Function是现有43个函数中基础的函数之一,简单的方法可以省略方法体{},和if的写法一样,但是复杂写法不可以省略{}。

	/**
	 * ([参数列表]) ->{
 	 * 	代码体;
 	 * }
 	 * 或
	 * ([参数列表]) ->代码体
	 *
	 */
    public static void main(String[] args) {
        //Function<T, R> 传入一个参数,并返回一个参数,两个参数类型需要自己传 可以对数据进行处理
        Function<String,Integer> stringIntegerAddFunction=(str)->Integer.parseInt(str)+1;
        System.out.println(stringIntegerAddFunction.apply("100"));
        Function<String,Integer> stringIntegerAddFunction2=(str)->{
            Integer integer=Integer.parseInt(str);
            return integer+1;
        };
        System.out.println(stringIntegerAddFunction2.apply("100"));
    }

两个写法虽然不一致,但是执行的结果是一样的。
简单Funciton对象的返回结果

compose方法、andThen方法和identity方法

使用匿名函数时,匿名函数接口的类中只允许存在一个方法,而之所有这三方法,是因为接口中可以通过关键字default定义默认方法,实现类如果不想要默认方法的实现逻辑可以根据需求重新定义,通过关键字static定义静态方法,实现类如果不想要静态方法的实现逻辑可以根据需求重新定义。

compose方法

有的时候,我们需要将两个或多个方法进行组合使用,这个时候就需要compose方法,compose会通过从右到左的顺序执行我们拼接的方法。

	/**
	 * ([参数列表]) ->{
 	 * 	代码体;
 	 * }
 	 * 或
	 * ([参数列表]) ->代码体
	 *
	 */
 public static void main(String[] args) {
        //Function<T, R> 传入一个参数,并返回一个参数,两个参数类型需要自己传 可以对数据进行处理
        Function<String,Integer> stringIntegerAddFunction=(str)->Integer.parseInt(str)+1;
        Function<Integer,String> integerStringFunction=integer -> String.valueOf(integer*2);
        System.out.println(stringIntegerAddFunction.compose(integerStringFunction).apply(100));
    }

可以通过结果看出,compose拼接会先执行被拼接的函数(integerStringFunction),再回去调用我们拼接的函数(stringIntegerAddFunction),为方便查看,我们请求的apply方法的类型已经发送了改变。
使用compose方法返回结果

andThen方法

andThen方法则刚好相反,这个函数会把拼接的函数从左到右执行。

	/**
	 * ([参数列表]) ->{
 	 * 	代码体;
 	 * }
 	 * 或
	 * ([参数列表]) ->代码体
	 *
	 */
    public static void main(String[] args) {
        //Function<T, R> 传入一个参数,并返回一个参数,两个参数类型需要自己传 可以对数据进行处理
        Function<String,Integer> stringIntegerAddFunction=(str)->Integer.parseInt(str)+1;
        Function<Integer,String> integerStringFunction=integer -> String.valueOf(integer*2);
        System.out.println(stringIntegerAddFunction.andThen(integerStringFunction).apply("100"));
    }

根据结果可以看出,andThen拼接会先执行被拼接的函数(stringIntegerAddFunction),再回去调用我们拼接的函数(integerStringFunction),为方便查看,我们请求的apply方法的类型已经发送了改变。
andThen方法返回结果

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腻&爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值