龙空技术网

Java Function和BiFunction详细介绍

迷路的架构师 366

前言:

此时小伙伴们对“functionjava”大致比较重视,各位老铁们都需要了解一些“functionjava”的相关资讯。那么小编也在网络上搜集了一些关于“functionjava””的相关资讯,希望小伙伴们能喜欢,咱们快快来学习一下吧!

介绍

在 Java 8 中,Function 和 BiFunction接口是内置的函数式接口。Function 接受一个参数并产生一个结果,BiFunction 则接受两个参数并产生一个结果。因为它们是函数式接口,因此可以用作lambda 表达式或方法引用的赋值目标。

在我们查看源码时,经常能看到他们两个函数的身影。比如 HashMap 源码:

Function 使用示例:

public V computeIfAbsent(K key,                         Function<? super K, ? extends V> mappingFunction) {  // 细节忽略}  

BiFunction 使用示例:

public V compute(K key,                 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {  // 细节忽略} 
Function详解

Function 函数式接口一共有四个方法,如下:

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

1、apply

Function<Integer, Integer> f1 = a -> a * 10;Integer result = f1.apply(5);System.out.println(result); // 输出:50

可知,apply方法是使用给定的参数来执行该函数,如上将 5 代入函数:5 * 10 刚好为 50。

2、compose

Function<Integer, Integer> f1 = a -> a * 10;Function<Integer, Integer> f2 = a -> a + 10;Function<Integer, Integer> compose = f1.compose(f2);Integer result = compose.apply(5);System.out.println(result); // 输出:150

可知,compose是将两个函数进行组合,compose 方法的参数(before)会优先执行,因此,如上执行顺序是 f2,然后才是 f1,最终结果是 150.

3、andThen

Function<Integer, Integer> f1 = a -> a * 10;Function<Integer, Integer> f2 = a -> a + 10;Integer result = f1.andThen(f2).apply(5);System.out.println(result); // 输出:60

从方法名可知,andThen 是后执行的意思,那么 f1.andThen(f2) 表示的意思是先执行 f1,再执行 f2,执行结果为 60。此处执行顺序刚好与 compose 方法相反。

4、identity

Function.identity() 返回一个输出跟输入一样的Lambda表达式对象。

从 identity 方法本身描述来说,我们可能会觉得该方法很鸡肋,不知道该干嘛。先看看示例:

Stream<String> stream = Stream.of("I", "love", "you", "too");Map<String, Integer> map = stream.collect(Collectors.toMap(Function.identity(), String::length));// map 的结果为:I:1love:4you:3too:3

可知,可用 Function.identity() 来作为 map 的 key 。

BiFunction详解

BiFunction 与 Function 稍微不同,它接收两个参数,并返回一个结果。

BiFunction 只有两个方法,如下:

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

1、apply

apply 的使用与 Function 的类似:

BiFunction<Integer, Integer, Integer> f3 = (a, b) -> a + b;Integer result = f3.apply(5, 10);System.out.println(result); // 输出:15

可知,对于输入参数5和10,执行 f3 函数后结果为 15。

2、andThen

andThen 的使用也与 Function 的相同,该方法接收一个 Function参数:

Function<Integer, Integer> f1 = a -> a * 10;BiFunction<Integer, Integer, Integer> f3 = (a, b) -> a + b;Integer result = f3.andThen(f1).apply(5, 10);System.out.println(result); // 输出:150

先执行 f3,然后再执行 f1,可得最终结果为 150。

标签: #functionjava #java中function用法 #java的function的作用