欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

java.util.function.Function的用法

发布时间:2025/3/12 编程问答 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 java.util.function.Function的用法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

JDK 1.8 API包含了很多内建的函数式接口,在老Java中常用到的比如Comparator或者Runnable接口,这些接口都增加了@FunctionalInterface注解以便能用在lambda上。现如今,我们则从Function常用函数入口,真正了解一下。

nametypedescription
ConsumerConsumer< T >接收T对象,不返回值
PredicatePredicate< T >接收T对象并返回boolean
FunctionFunction< T, R >接收T对象,返回R对象
SupplierSupplier< T >提供T对象(例如工厂),不接收值
UnaryOperatorUnaryOperator接收T对象,返回T对象
BinaryOperatorBinaryOperator接收两个T对象,返回T对象

标注为FunctionalInterface的接口被称为函数式接口,该接口只能有一个自定义方法,但是可以包括从object类继承而来的方法。如果一个接口只有一个方法,则编译器会认为这就是一个函数式接口。是否是一个函数式接口,需要注意的有以下几点:

该注解只能标记在”有且仅有一个抽象方法”的接口上。

JDK8接口中的静态方法和默认方法,都不算是抽象方法。

接口默认继承java.lang.Object,所以如果接口显示声明覆盖了Object中方法,那么也不算抽象方法。
该注解不是必须的,如果一个接口符合”函数式接口”定义,那么加不加该注解都没有影响。加上该注解能够更好地让编译器进行检查。如果编写的不是函数式接口,但是加上了@FunctionInterface,那么编译器会报错。
在一个接口中定义两个自定义的方法,就会产生Invalid ‘@FunctionalInterface’ annotation; FunctionalInterfaceTest is not a functional interface错误.
Function常用方法&&实践

//将Function对象应用到输入的参数上,然后返回计算结果。

R apply(T t);

例子1,先来个简单版的:传String,返回String。

package org.dreams.transaction;import java.util.function.Function;public class FunctionTest<In, Out> {private Function<In, Out> processor = new Function<In, Out>() {@Overridepublic Out apply(In in) {return (Out) new String("apply:" + in);}};public static void main(String[] args) {FunctionTest<String, String> functionTest = new FunctionTest();System.out.println(functionTest.processor.apply("hello~!"));} } lambda表达式的写法:package org.dreams.transaction;import java.util.function.Function;public class FunctionTest<In, Out> {private Function<In, Out> processor = in -> {return (Out) new String("apply:" + in);};public static void main(String[] args) {FunctionTest<String, String> functionTest = new FunctionTest();System.out.println(functionTest.processor.apply("hello~!"));} }

例子二:
andThen方法

//返回一个先执行当前函数对象apply方法再执行after函数对象apply方法的函数对象。 default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {Objects.requireNonNull(after);return (T t) -> after.apply(apply(t));}

compose方法

//返回一个先执行before函数对象apply方法再执行当前函数对象apply方法的函数对象 default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {Objects.requireNonNull(before);return (V v) -> apply(before.apply(v));}

完整代码

package org.dreams.transaction.java8;import java.util.function.Function;public class Function2Test{public static void main(String[] args) {Function<Integer, Integer> name = e -> e * 2;Function<Integer, Integer> square = e -> e * e;int value = name.andThen(square).apply(3);System.out.println("andThen value=" + value);int value2 = name.compose(square).apply(3);System.out.println("compose value2=" + value2);//返回一个执行了apply()方法之后只会返回输入参数的函数对象Object identity = Function.identity().apply("huohuo");System.out.println(identity);} }

返回结果

andThen value=36 compose value2=18 huohuo

总结

以上是生活随笔为你收集整理的java.util.function.Function的用法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。