南京网站推广营销公司,为什么网站之有首页被收录,wordpress升级失败,成都上市的网站建设公司众所周知#xff0c;Java8提供了一下非常重要的函数式接口。今天我们就来讲讲其中一个函数式接口-----Function接口。 下面的代码就是Function接口的全部代码。接下来我们逐个分析一下。
FunctionalInterface
public interface FunctionT, RFunctionalInterface 表明…众所周知Java8提供了一下非常重要的函数式接口。今天我们就来讲讲其中一个函数式接口-----Function接口。 下面的代码就是Function接口的全部代码。接下来我们逐个分析一下。
FunctionalInterface
public interface FunctionT, RFunctionalInterface 表明该接口是一个函数式接口 T, R T 代表参数R 代表返回值。
第一个方法------R apply(T t)
示例代码
public class FunctionTest {public static void main(String[] args) {FunctionTest functionTest new FunctionTest();System.out.println(functionTest.compute(1, value - 2 * value));}public int compute(int a, FunctionInteger, Integer function) {int result function.apply(a);return result;}
}上述代码展示的就是Function接口中apply方法的使用其中通过 functionTest.compute方法将两个参数传过去在compute方法中function使用参数进行运算。
第二个方法 ------- compose
这个方法是一个默认方法。为了方便在接口中实现具体逻辑而添加的关键字。 我们可以看到compose方法有一个输入参数为Function类型的值并且也会返回一个Function类型的值重点我们看最后一句就会发现它是先调用参数的apply 方法 再调用调用者的apply 方法
default V FunctionV, R compose(Function? super V, ? extends T before) {Objects.requireNonNull(before);return (V v) - apply(before.apply(v));}通过上面的分析可以得出结果----12即对参数先执行 value * value在执行 value * 3。
System.out.println(functionTest2.compute(2, value - value * 3, value - value * value));public int compute(int a, FunctionInteger, Integer function1, FunctionInteger, Integer function2) {return function1.compose(function2).apply(a);}第三个方法 ------ andThen
大家是不是觉得这个方法和上面的compose方法差不多唯一的差别就是最后一句。该方法执行的顺序就是先执行调用者的apply方法在执行参数的apply方法。与compose方法相反
default V FunctionT, V andThen(Function? super R, ? extends V after) {Objects.requireNonNull(after);return (T t) - after.apply(apply(t));}示例代码 经过上面的分析我们很容易得出结果值为36。
System.out.println(functionTest2.compute(2, value - value * 3, value - value * value));public int compute2(int a, FunctionInteger, Integer function1, FunctionInteger, Integer function2) {return function1.andThen(function2).apply(a);}第四个方法 ------ identity
static T FunctionT, T identity() {return t - t;}该行代码因为实现了Function中唯一的抽象方法所以通过下面这行代码实例化出了一个 “接口” 对象同样也会输出helloworld
FunctionString, String fun1 Function.identity();String str1 fun1.apply(helloWorld);System.out.println(str1);看完了Function相信大家在学习Java8的时候都看到了还有一个叫BiFunction的类下面我们来分析一下BiFunction
什么是BiFunction
FunctionalInterface
public interface BiFunctionT, U, R它也是一个函数式接口它是Function 的 另一种形式它接收两个参数TU返回一个参数R。那么接下来我们看看这个接口里面有哪些方法吧。
第一个方法 ------ apply
R apply(T t, U u);示例代码
System.out.println(functionTest2.compute4(2, 3, (value1, value2) - value1 value2));public int compute4(int a, int b, BiFunctionInteger, Integer, Integer biFunction) {return biFunction.apply(a, b);}上述代码使用的就是apply方法我们发现其实它跟Function里面的apply其实一样也就是多了一个参数而已。接下来我们看第二个方法。
第二个方法 ----- andThen
源码
default V BiFunctionT, U, V andThen(Function? super R, ? extends V after) {Objects.requireNonNull(after);return (T t, U u) - after.apply(apply(t, u));}我们发现andThen方法里面的参数是Function类型的那为什么不能是BiFunction类型的呢。 andThen是先执行调用者的apply也就是说他先调用BiFunction的apply方法再去调用参数的apply方法其中根据Java的规范一个方法只能有一个返回值。所以当执行参数的apply方法的时候只能有一个参数所以andThen方法里面的参数必须是Function类型的。
那么大家可能会问了为什么BiFunction里面没有compose方法。 根据Function接口里面的compose方法我们可以分析出它是先执行参数的apply方法在执行调用者的apply方法。 假如BiFunction里面有compose又因为BiFunction接受两个参数那么通过上面的分析可以得出它的compose方法的参数必定是BiFunction类型的但是下一步执行调用者的apply方法的时候因为只有一个参数所以是无法满足的。所以BiFunction里面没有compose方法。 源码
public interface FunctionT, R {/*** 将此函数应用于给定参数* 真正执行函数接口的方法*/R apply(T t);/*** 函数链before执行的结果做根函数为参数*/default V FunctionV, R compose(Function? super V, ? extends T before) {Objects.requireNonNull(before);return (V v) - apply(before.apply(v));}/*** 函数链根函数执行结果做为after的参数*/default V FunctionT, V andThen(Function? super R, ? extends V after) {Objects.requireNonNull(after);return (T t) - after.apply(apply(t));}/*** 返回一个参数做为返回值的函数*/static T FunctionT, T identity() {return t - t;}
}看注释有些绕还是看例子
//例1
public class FunctionDemo {public static void main(String[] args) {FunctionInteger,Integer root (v) - {System.out.println(root apply);return v 1;// 12};FunctionInteger,Integer before (v) - {System.out.println(before apply);return v 1;// 先得结果11再传给root};System.out.println(root.compose(before).apply(10));// before apply// root apply// 12}
}//例2
public class FunctionDemo {public static void main(String[] args) {FunctionInteger,Integer root (v) - {System.out.println(root apply);return v 1;// 先得结果11再传给after};FunctionInteger,Integer after (v) - {System.out.println(after apply);return v 1;// 12};System.out.println(root.andThen(after).apply(10));// root apply// after apply// 12}
}例1和例2虽然参数一样结果一样但是相对与根函数root执行的顺序一个在前一个在后。
Function与Consumer接口都有andThen方法两者的区别是对参数的使用方式不同。每Consumer使用的是同一个原始参数Function的参数只会被根函数使用一次之后的函数使用的是前一个函数的结果做为参数。
了解了Function其他的也就一目了然了
BiFunctionT,U,R代表了一个接受两个输入参数的方法并且返回一个结果DoubleFunctionR代表接受一个double值参数的方法并且返回结果DoubleToIntFunction接受一个double类型输入返回一个int类型结果。DoubleToLongFunction接受一个double类型输入返回一个long类型结果IntFunctionR:接受一个int类型输入参数返回一个结果 。IntToDoubleFunction接受一个int类型输入返回一个double类型结果 。IntToLongFunction接受一个int类型输入返回一个long类型结果。LongFunctionR 接受一个long类型输入参数返回一个结果。LongToDoubleFunction 接受一个long类型输入返回一个double类型结果。LongToIntFunction接受一个long类型输入返回一个int类型结果。ToDoubleBiFunctionT,U接受两个输入参数返回一个double类型结果ToDoubleFunctionT接受一个输入参数返回一个double类型结果ToIntBiFunctionT,U接受两个输入参数返回一个int类型结果。ToIntFunctionT接受一个输入参数返回一个int类型结果。ToLongBiFunctionT,U接受两个输入参数返回一个long类型结果。ToLongFunctionT接受一个输入参数返回一个long类型结果。