java.util.function.Function的用法
JDK 1.8 API包含了很多內(nèi)建的函數(shù)式接口,在老Java中常用到的比如Comparator或者Runnable接口,這些接口都增加了@FunctionalInterface注解以便能用在lambda上?,F(xiàn)如今,我們則從Function常用函數(shù)入口,真正了解一下。
| Consumer | Consumer< T > | 接收T對(duì)象,不返回值 |
| Predicate | Predicate< T > | 接收T對(duì)象并返回boolean |
| Function | Function< T, R > | 接收T對(duì)象,返回R對(duì)象 |
| Supplier | Supplier< T > | 提供T對(duì)象(例如工廠),不接收值 |
| UnaryOperator | UnaryOperator | 接收T對(duì)象,返回T對(duì)象 |
| BinaryOperator | BinaryOperator | 接收兩個(gè)T對(duì)象,返回T對(duì)象 |
標(biāo)注為FunctionalInterface的接口被稱為函數(shù)式接口,該接口只能有一個(gè)自定義方法,但是可以包括從object類繼承而來的方法。如果一個(gè)接口只有一個(gè)方法,則編譯器會(huì)認(rèn)為這就是一個(gè)函數(shù)式接口。是否是一個(gè)函數(shù)式接口,需要注意的有以下幾點(diǎn):
該注解只能標(biāo)記在”有且僅有一個(gè)抽象方法”的接口上。
JDK8接口中的靜態(tài)方法和默認(rèn)方法,都不算是抽象方法。
接口默認(rèn)繼承java.lang.Object,所以如果接口顯示聲明覆蓋了Object中方法,那么也不算抽象方法。
該注解不是必須的,如果一個(gè)接口符合”函數(shù)式接口”定義,那么加不加該注解都沒有影響。加上該注解能夠更好地讓編譯器進(jìn)行檢查。如果編寫的不是函數(shù)式接口,但是加上了@FunctionInterface,那么編譯器會(huì)報(bào)錯(cuò)。
在一個(gè)接口中定義兩個(gè)自定義的方法,就會(huì)產(chǎn)生Invalid ‘@FunctionalInterface’ annotation; FunctionalInterfaceTest is not a functional interface錯(cuò)誤.
Function常用方法&&實(shí)踐
//將Function對(duì)象應(yīng)用到輸入的參數(shù)上,然后返回計(jì)算結(jié)果。
R apply(T t);例子1,先來個(gè)簡(jiǎn)單版的:傳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表達(dá)式的寫法: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方法
compose方法
//返回一個(gè)先執(zhí)行before函數(shù)對(duì)象apply方法再執(zhí)行當(dāng)前函數(shù)對(duì)象apply方法的函數(shù)對(duì)象 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);//返回一個(gè)執(zhí)行了apply()方法之后只會(huì)返回輸入?yún)?shù)的函數(shù)對(duì)象Object identity = Function.identity().apply("huohuo");System.out.println(identity);} }返回結(jié)果
andThen value=36 compose value2=18 huohuo總結(jié)
以上是生活随笔為你收集整理的java.util.function.Function的用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的世界minecraft-Python
- 下一篇: JAVA中和、||和|的区别?