Java8 函数式接口之Function 与 BiFunction

1,749 阅读2分钟

前言

今天开始回炉 java8 系列,虽然这个主题已经被好多人写过,出于对自己知识体系的补充和完善,还是决定好好整理一下。这次学习的两个函数式接口分别是 Funtion 和 BiFunction

实战

Function

  • 先看下具体如何使用Funtion 吧
	@Test
  public void test() {
    Function<Integer, Integer> f1 = x -> {return x * x;};
    Function<Integer, Integer> f2 = x -> {return x + x;};

    // compose 被组合的函数优先执行
    //(3+3)*(3+3)= 36
    System.out.println(f1.compose(f2).apply(3));
    //(3*3)+(3*3)=18
    System.out.println(f1.andThen(f2).apply(3));
  }
  • Function 中比较常用的也就是以下三个方法,接口定义如下
    • apply,
      • 接收一个参数,返回一个值
    • compose,
      • 是一个默认方法
      • 用于两个 function组合,例如 f1.compose(f2) 则被调用的 f2 将先被执行
    • andThen
      • 顾名思义就是接下去的意思,f1.andThen(f2) 则表示先执行f1,再执行 f2
		/**
     * 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));
    }

BiFuntion

  • 如何使用
// 定义一个 biFunction
BiFunction<Integer, Integer, Integer> biFunction = (x1, x2) -> x1 + x2;
Integer apply = biFunction.apply(1, 2);
System.out.println(apply); // 3


	@Test
  public void testBiFunc1(){
    int i = compute4(2, 3, (v1, v2) -> v1 + v2, (v1) -> v1 * 2);
    System.out.println(i);// 10
  }
  • apply方法不必多说,接收 t,u 返回 R,其中 R 是一个 function
  • andThen, 入参是一个 Function
    /**
     * Applies this function to the given arguments.
     *
     * @param t the first function argument
     * @param u the second function argument
     * @return the function result
     */
    R apply(T t, U u);

    /**
     * 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
     */
    default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t, U u) -> after.apply(apply(t, u));
    }