暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Java新特性解读JDK8之函数式接口BiFunction

227decision 2020-01-15
1432

上一篇文章介绍了Function,只可以传递一个参数,如果要传递两个参数,则可以使用BiFunction。

BiFunction源码

    package java.util.function;


    import java.util.Objects;


    /**
    * Represents a function that accepts two arguments and produces a result.
    * This is the two-arity specialization of {@link Function}.
    *
    * <p>This is a <a href="package-summary.html">functional interface</a>
    * whose functional method is {@link #apply(Object, Object)}.
    *
    * @param <T> the type of the first argument to the function
    * @param <U> the type of the second argument to the function
    * @param <R> the type of the result of the function
    *
    * @see Function
    * @since 1.8
    */
    @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);
    }

    BiFunction使用

      package com.example.functionalinterfacedemo;


      import java.util.function.BiFunction;
      /**
      * BiFunction 使用例子
      * @author thehe
      *
      */
      public class BiFunctionDemo {


      public static void main(String[] args) {
      System.out.println(test(1,2,(x,y)->x+y));
      System.out.println(test(1,2,(x,y)->x-y));
      System.out.println(test(1,2,(x,y)->x*y));
      System.out.println(test(1,2,(x,y)->x/y));
      }
      public static Integer test(Integer integer,Integer integer2,BiFunction<Integer, Integer, Integer> biFunction){
      return biFunction.apply(integer, integer2);
      }


      }



      最后修改时间:2020-07-01 13:37:31
      文章转载自227decision,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

      评论