日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java.util.function.Function的用法

發布時間:2025/3/12 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。