BiConsumer跟BiFunction的區別詳解

Comsumer和BiConsumer方法:

首先看一下兩個接口:幾乎差不多,就是方法參數多一個.BiConsumer可以看做Consumer的增強吧!
在這裏插入圖片描述
在這裏插入圖片描述
Consumer的作用就是定義一個函數,然後對其進行消費處理,(accept方法);
而andThen方法相當於是組合兩個方法,返回一個新的方法,是先對給定的參數進行定義的操作然後在執行after操作;
BiConsumer就是Consumer的加強版,處理兩個參數,默認方法andThen也是一樣的:寫個測試:

@Test
  public void test41() throws Exception {
    Consumer<Integer> action1 = (x) -> {
      System.out.println("對傳進來的進行加1操作: " + (x + 1));
    };
    Consumer<Integer> action2 = (x) -> {
      System.out.println("對傳進來的進行減1操作: " + (x - 1));
    };
    Consumer<Integer> anction3 = action1.andThen(action2);

    //先執行加法在執行減法
    System.out.println("執行anction1");
    action1.accept(3);
    System.out.println("執行anction2");
    action2.accept(3);
    System.out.println("執行anction3");
    anction3.accept(3);

  }

  @Test
  public void test42() throws Exception {
    BiConsumer<Integer, Integer> action1 = (x, y) -> {
      System.out.println("對傳進來的進行相加操作: " + (x + y));
    };
    BiConsumer<Integer, Integer> action2 = (x, y) -> {
      System.out.println("對傳進來的進行相減操作: " + (x - y));
    };
    BiConsumer<Integer, Integer> anction3 = action1.andThen(action2);

    //先執行加法在執行減法
    System.out.println("執行anction1");
    action1.accept(1, 1);
    System.out.println("執行anction2");
    action2.accept(1, 1);
    System.out.println("執行anction3");
    anction3.accept(1, 1);

  }

執行結果:
在這裏插入圖片描述
在這裏插入圖片描述

Function和BiFunction的區別

先看源碼,其實感覺跟Consumer都差不多:

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

BiFunction的源碼:

@FunctionalInterface
public interface BiFunction<T, U, R> {

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

先說function,function主要就apply方法,接收參數爲T返回R類型的;
compose,和andThen都是差不多的,都是組合成一個新的函數,然後按照不同順序執行…
identity是一個靜態方法,註釋也寫的很明瞭了,返回接收的參數的函數,就是接收啥返回對應的函數;

 @Test
  public void test51() throws Exception {

    Function<Integer, Integer> action1 = (x) -> {
      System.out.println("對傳進來的進行加1操作: " + (x + 1));
      return x + 1;
    };
    Function<Integer, Integer> action2 = (x) -> {
      System.out.println("對傳進來的進行-1操作: " + (x - 1));
      return (x - 1);
    };
    Function<Integer, Integer> action3 = action1.andThen(action2);
    Function<Integer, Integer> compose = action1.compose(action2);

    //先執行加法在執行減法
    System.out.println("執行anction1");
    action1.apply(1);
    System.out.println("執行anction2");
    action2.apply(1);
    System.out.println("執行andThen返回的函數");
    action3.apply(1);
    System.out.println("執行compose返回的函數");
    compose.apply(1);

//Function.identity()返回一個輸出跟輸入一樣的Lambda表達式對象,等價於形如t -> t形式的Lambda表達式
    Stream<String> stream = Stream.of("I", "love", "you", "too");
    //這三個相同的作用...就是返回的是原來的字符串.
//    Map<String, Integer> map = stream.collect(Collectors.toMap(Function.identity(), String::length));
    Map<String, Integer> map = stream.collect(Collectors.toMap(String::toString, String::length));
//    Map<String, Integer> map = stream.collect(Collectors.toMap(str->str, String::length));
    System.out.println(map);
    
  }

執行結果:
在這裏插入圖片描述

BiFunction跟BiConsumer差不多,都是對原來的進行增強,多加了個參數,都是差不多的…注意BiFunction的andThen方法是傳入的Function,

  @Test
  public void test52() throws Exception {
    BiFunction<Integer, Integer, Integer> action = (x, y) -> {
      System.out.println(x + y);
      return x + y;
    };
    Function<Integer, Integer> action2 = (x) -> {
      System.out.println("Function..." + x);
      return x;
    };
    BiFunction<Integer, Integer, Integer> integerIntegerBiConsumer = action.andThen(action2);

    integerIntegerBiConsumer.apply(1, 1);

  }

fuction包下

有很多擴展的接口,比如DoubleConsumer,請求參數固定是dubbo,等…
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章