Function与BiFunction接口
Function与BiFunction接口方法详解
Function接口
Function接口的源码:
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
static <T> Function<T, T> identity() {
return t -> t;
}
}
首先public interface Function<T, R>
,其中T表示接受的对象,返回R对象
接口中第一个方法
R apply(T t);
方法说明:表示将Function对象应用到输入的参数上,返回计算的结果,查看以下代码
public class TestOne {
public static void main(String[] args) {
System.out.println(testApply(4,value -> value * value));//16
}
public static int testApply(int param,Function<Integer,Integer> function){
return function.apply(param);
}
}
根据说明可以知道,将Function对象value -> value * value
应用到输入的参数param上,也就是param = param *param
,所以结果输出为16
接口中第二个方法
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
方法说明:
首先compose()方法接收到一个Function对象赋值给before,然后调用Object.require(before)
判断before是否为空,如果为空则抛出NullPointerException,随后先执行before.apply()
方法,并且将返回的参数值作为当前的Function对象的apply()方法的参数值再进行计算,查看以下代码:
public class Test {
public static void main(String[] args) {
System.out.println(compute(2,value -> value * 3,value -> value * value));//12
}
public static int compute(int param, Function<Integer,Integer> function1,
Function<Integer,Integer> function2){
return function1.compose(function2).apply(param);
}
}
根据方法说明可以得知,方法先执行function2.apply(2)
,将function2对象value -> value * value
作用与参数param,并得到返回值4
然后将返回值4作为function1.apply()
的参数,也就是将function对象value -> value * 3
作用与参数值4中,最后得到返回值12,
所以输出结果为12
接口中第三个方法:
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
方法说明:
首先compose()方法接收到一个Function对象赋值给before,然后同样调用Object.require(after)
判断after是否为空,如果为空则抛出NullPointerException,随后先执行当前对象的after.apply()
方法,并且将返回的参数值作为传入的对象的apply()方法的参数值再进行计算,查看以下代码:
public class Test05 {
public static void main(String[] args) {
System.out.println(computer(2,value -> value * 3,value -> value * value));//36
}
public static int computer(int param, Function<Integer,Integer> function1,
Function<Integer,Integer> function2){
return function1.andThen(function2).apply(param);
}
}
根据方法说明可以得知,方法先执行function1.apply(2)
,将function1对象value -> value * 3
作用与参数param,并得到返回值6
然后将返回值6作为function2.apply()
的参数,也就是将function2对象value -> value * value
作用与参数值6中,最后得到返回值36,
所以输出结果为36
BiFunction接口
Bifunction接口的源码:
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
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));
}
}
其中public interface BiFunction<T, U, R>
,其中T表示接受的第一个对象,U表示接受的第二个对象,返回R对象
接口中第一个方法
R apply(T t, U u);
方法说明:
将Function对象应用到参数t和u上,并返回结果R,查看以下代码:
public class Test06 {
public static void main(String[] args) {
System.out.println(compare(5,6,(value1,value2) -> value1 * value2));//30
}
public static int compare(int a,int b,BiFunction<Integer,Integer,Integer> biFunction){
return biFunction.apply(a,b);
}
}
根据方法说明,可以得知首先将bifunction对象(value1,value2) -> value1 * value2
应用到参数a和b中,也就是R -> a * b
然后将计算出的结果返回,所以结果为30
接口中第二个方法
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));
}
方法说明:
首先compose()方法接收到一个Function对象赋值给before,然后同样调用Object.require(after)
判断after是否为空,如果为空则抛出NullPointerException,随后先执行当前对象的after.apply()
方法,并且将返回的参数值作为传入的对象的apply()方法的参数值再进行计算,查看以下代码:
public class Test06 {
public static void main(String[] args) {
System.out.println(compare(2,3,(value1,value2) -> value1 * value2,
value -> value + 2));
}
public static int compare(
int a, int b,
BiFunction<Integer,Integer,Integer> biFunction,
Function<Integer,Integer> function){
return biFunction.andThen(function).apply(a,b);
}
}
根据方法说明可以得知,方法先执行biFunction.apply(a,b)
,将biFunction对象(value1,value2) -> value1 * value2
作用于参数a和b,并得到返回值6
然后将返回值6作为function.apply()
的参数,也就是将function对象value -> value + 2
作用与参数值6中,最后得到返回值8,
所以输出结果为8
为什么Function接口中有compose(),而BiFunction接口中没有呢?
很简单,因为BiFunction接口的apply()方法中需要传入两个参数,而方法的返回值只有一个值