Function接口 – Java8中java.util.function包下的函数式接口

    xiaoxiao2024-08-20  82

    早先我写了一篇《函数式接口》,探讨了Java8中函数式接口的用法。如果你正在浏览Java8的API,你会发现java.util.function中 Function, Supplier, Consumer, Predicate和其他函数式接口广泛用在支持lambda表达式的API中。这些接口有一个抽象方法,会被lambda表达式的定义所覆盖。在这篇文章中,我会简单描述Function接口,该接口目前已发布在java.util.function中。

    Function接口的主要方法:

    R apply(T t) – 将Function对象应用到输入的参数上,然后返回计算结果。

    default ‹V› Function‹T,V› – 将两个Function整合,并返回一个能够执行两个Function对象功能的Function对象。

    译者注:Function接口中除了apply()之外全部接口如下:

    default <V> Function<T,V> andThen(Function<? super R,? extends V> after) 返回一个先执行当前函数对象apply方法再执行after函数对象apply方法的函数对象。

    default <V> Function<T,V> compose(Function<? super V,? extends T> before)返回一个先执行before函数对象apply方法再执行当前函数对象apply方法的函数对象。

    static <T> Function<T,T> identity() 返回一个执行了apply()方法之后只会返回输入参数的函数对象。

    本章节将会通过创建接受Function接口和参数并调用相应方法的例子探讨apply方法的使用。我们同样能够看到API的调用者如何利用lambda表达式替代接口的实现。除了传递lambda表达式之外,API使用者同样可以传递方法的引用,但这样的例子不在本篇文章中。

    如果你想把接受一些输入参数并将对输入参数处理过后的结果返回的功能封装到一个方法内,Function接口是一个不错的选择。输入的参数类型和输出的结果类型可以一致或者不一致。

    一起来看看接受Function接口实现作为参数的方法的例子:

    01 public class FunctionDemo { 02  03     //API which accepts an implementation of 04  05     //Function interface 06  07     static void modifyTheValue(int valueToBeOperated, Function<Integer, Integer> function){ 08  09         int newValue = function.apply(valueToBeOperated); 10  11         /*      12          * Do some operations using the new value.      13          */ 14  15         System.out.println(newValue); 16  17     } 18  19}

    下面是调用上述方法的例子:

    01 public static void main(String[] args) { 02  03     int incr = 20;  int myNumber = 10; 04  05     modifyTheValue(myNumber, val-> val + incr); 06  07     myNumber = 15;  modifyTheValue(myNumber, val-> val * 10); 08  09     modifyTheValue(myNumber, val-> val - 100); 10  11     modifyTheValue(myNumber, val-> "somestring".length() + val - 100); 12  13}

    你可以看到,接受1个参数并返回执行结果的lambda表达式创建在例子中。这个例子的输入如下:

    130 2  3150 4  5 -85 6  7 -75

     

    转载自 并发编程网 - ifeve.com 相关资源:敏捷开发V1.0.pptx
    最新回复(0)