JDK8新特性-java.util.function-Function接口使用
JDK8新特性-java.util.function-Function接口
14年,Oracle公司如期發(fā)布了Java 8正式版?,F(xiàn)如今4年過去了,終于鼓起勇氣認(rèn)真對待它,就好似雖然認(rèn)識了好幾年的伙伴,突然感覺要成為情侶的感覺……
JDK 1.8 API包含了很多內(nèi)建的函數(shù)式接口,在老Java中常用到的比如Comparator或者Runnable接口,這些接口都增加了@FunctionalInterface注解以便能用在lambda上。
現(xiàn)如今,我們則從Function常用函數(shù)入口,真正了解一下。
| name | type | description |
|---|---|---|
| Consumer | Consumer< T > | 接收T對象,不返回值 |
| Predicate | Predicate< T > | 接收T對象并返回boolean |
| Function | Function< T, R > | 接收T對象,返回R對象 |
| Supplier | Supplier< T > | 提供T對象(例如工廠),不接收值 |
| UnaryOperator | UnaryOperator | 接收T對象,返回T對象 |
| BinaryOperator | BinaryOperator | 接收兩個T對象,返回T對象 |
標(biāo)注為FunctionalInterface的接口被稱為函數(shù)式接口,該接口只能有一個自定義方法,但是可以包括從object類繼承而來的方法。
如果一個接口只有一個方法,則編譯器會認(rèn)為這就是一個函數(shù)式接口。
是否是一個函數(shù)式接口,需要注意的有以下幾點:
- 該注解只能標(biāo)記在”有且僅有一個抽象方法”的接口上。
- JDK8接口中的靜態(tài)方法和默認(rèn)方法,都不算是抽象方法。
- 接口默認(rèn)繼承java.lang.Object,所以如果接口顯示聲明覆蓋了Object中方法,那么也不算抽象方法。
- 該注解不是必須的,如果一個接口符合”函數(shù)式接口”定義,那么加不加該注解都沒有影響。加上該注解能夠更好地讓編譯器進(jìn)行檢查。如果編寫的不是函數(shù)式接口,但是加上了@FunctionInterface,那么編譯器會報錯。
- 在一個接口中定義兩個自定義的方法,就會產(chǎn)生Invalid ‘@FunctionalInterface’ annotation; FunctionalInterfaceTest is not a functional interface錯誤.
Function常用方法&&實踐
//將Function對象應(yīng)用到輸入的參數(shù)上,然后返回計算結(jié)果。 R apply(T t);
//返回一個先執(zhí)行當(dāng)前函數(shù)對象apply方法再執(zhí)行after函數(shù)對象apply方法的函數(shù)對象。
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}//返回一個先執(zhí)行before函數(shù)對象apply方法再執(zhí)行當(dāng)前函數(shù)對象apply方法的函數(shù)對象
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}compose 和 andThen 的不同之處是函數(shù)執(zhí)行的順序不同。
compose 函數(shù)先執(zhí)行參數(shù),然后執(zhí)行調(diào)用者,而 andThen 先執(zhí)行調(diào)用者,然后再執(zhí)行參數(shù)。
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);
//返回一個執(zhí)行了apply()方法之后只會返回輸入?yún)?shù)的函數(shù)對象
Object identity = Function.identity().apply("huohuo");
System.out.println(identity);
}直接看結(jié)果:
andThen value=36
compose value2=18
huohuo
apply基本應(yīng)用
字符串長度記錄返回
public class MyFunction implements Function<String,Integer>{
@Override
public Integer apply(String s) {
return s.length();
}
}返回兩個字符串的連接,BiFunction與Function的不同就是傳入兩個參數(shù),依舊返回一個值。
public class MyBiFunction implements BiFunction<String, String, String> {
@Override
public String apply(String s, String s2) {
return s+";"+s2;
}
}最后調(diào)用結(jié)果:
private static String hello = "Nice to meet you";
private static String name = "my name is huohuo";
public static void main(String[] args) {
MyFunction myFunction = new MyFunction();
MyBiFunction biFunction = new MyBiFunction();
int num = myFunction.apply(hello);
String valueBi = biFunction.apply(hello, name);
//hello長度返回
System.out.println(num);
//語句整合返回
System.out.println(valueBi);
}返回值:
16
Nice to meet you;my name is huohuo
其實使用的過程感覺這些都無所必要,但是對于新特性以及代碼規(guī)范而言,即使是簡易代碼,也有了一個整合的過程。
Function簡單實踐僅此為止,下篇文章講述Predicate的使用。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java Mybatis框架Dao層的實現(xiàn)與映射文件以及核心配置文件詳解分析
MyBatis 是一款優(yōu)秀的持久層框架,它支持自定義 SQL、存儲過程以及高級映射。MyBatis 免除了幾乎所有的 JDBC 代碼以及設(shè)置參數(shù)和獲取結(jié)果集的工作。MyBatis 可以通過簡單的 XML 或注解來配置和映射原始類型、接口和 Java POJO為數(shù)據(jù)庫中的記錄2021-10-10
Spring中PathMatcher路徑匹配器的實現(xiàn)
Spring框架中的PathMatcher是一個接口,本文主要介紹了Spring中PathMatcher路徑匹配器的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
Spring Data JPA中 in 條件參數(shù)的傳遞方式
這篇文章主要介紹了Spring Data JPA中 in 條件參數(shù)的傳遞方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
使用Java實現(xiàn)MapReduce詞頻統(tǒng)計示例代碼
這篇文章主要介紹了使用Java實現(xiàn)MapReduce詞頻統(tǒng)計的相關(guān)資料,通過詞頻統(tǒng)計示例來展示MapReduce的運行機(jī)制,涵蓋了Mapper和Reducer的實現(xiàn),并說明了如何配置和執(zhí)行MapReduce作業(yè),需要的朋友可以參考下2024-11-11
Spring?AOP操作的相關(guān)術(shù)語及環(huán)境準(zhǔn)備
這篇文章主要為大家介紹了Spring?AOP操作的相關(guān)術(shù)語及環(huán)境準(zhǔn)備學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

