Java中Function的使用及說明
Java Function的使用
一、方法介紹
表示接受一個(gè)參數(shù)并產(chǎn)生結(jié)果的函數(shù)。
參數(shù)類型
- T - 函數(shù)輸入的類型
- R - 函數(shù)的結(jié)果類型
方法介紹
R apply(T t)
將此函數(shù)應(yīng)用于給定的參數(shù)。
default Function<V, R> compose(Function<? super V, ? extends T> before)
返回一個(gè)組合函數(shù),首先將before函數(shù)應(yīng)用于其輸入,然后將此函數(shù)應(yīng)用于結(jié)果。 如果任一函數(shù)的評估引發(fā)異常,則將其轉(zhuǎn)發(fā)給組合函數(shù)的調(diào)用者。
default Function<T, V> andThen(Function<? super R, ? extends V> after)
返回一個(gè)組合函數(shù),首先將此函數(shù)應(yīng)用于其輸入,然后將after函數(shù)應(yīng)用于結(jié)果。 如果任一函數(shù)的評估引發(fā)異常,則將其轉(zhuǎn)發(fā)給組合函數(shù)的調(diào)用者。
static Function<T, T> identity()
返回一個(gè)總是返回其輸入?yún)?shù)的函數(shù)。
源碼
@FunctionalInterface public interface Function<T, R> { /** * Applies this function to the given argument. * * @param t the function argument * @return the function result */ R apply(T t); /** * Returns a composed function that first applies the {@code before} * function to its input, and then applies this function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <V> the type of input to the {@code before} function, and to the * composed function * @param before the function to apply before this function is applied * @return a composed function that first applies the {@code before} * function and then applies this function * @throws NullPointerException if before is null * * @see #andThen(Function) */ default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v)); } /** * Returns a composed function that first applies this function to * its input, and then applies the {@code after} function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <V> the type of output of the {@code after} function, and of the * composed function * @param after the function to apply after this function is applied * @return a composed function that first applies this function and then * applies the {@code after} function * @throws NullPointerException if after is null * * @see #compose(Function) */ default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); } /** * Returns a function that always returns its input argument. * * @param <T> the type of the input and output objects to the function * @return a function that always returns its input argument */ static <T> Function<T, T> identity() { return t -> t; } }
二、demo
public class Test { public static void main(String[] args) throws Exception { Function<Integer, Integer> add = p -> p + 10; Integer result = add.apply(10); // 這里會(huì)輸出 20,因?yàn)檫@個(gè)函數(shù)定義的操作時(shí)把參數(shù)加上 10 后返回 System.out.println(result); Function<Integer, Integer> multiplyTen = a -> a * 10; Function<Integer, Integer> addTen = a -> a + 10; // 先增加 10,然后再乘 10,輸出結(jié)果 110 Function<Integer, Integer> addTenThenMultiplyTen = multiplyTen.compose(addTen); System.out.println(addTenThenMultiplyTen.apply(1)); // 先乘 10,然后再加 10,輸出結(jié)果 20 Function<Integer, Integer> multiplyTenAddTenThen = multiplyTen.andThen(addTen); System.out.println(multiplyTenAddTenThen.apply(1)); } }
結(jié)果
Java內(nèi)置函數(shù) Function函數(shù)
Java內(nèi)置Function參數(shù),類包是在 java.base 模塊下 java.util.function 包中,其方法主要用于對一個(gè)請求參數(shù)的處理,并返回一個(gè)結(jié)果。
Function源碼
package java.util.function; import java.util.Objects; /** * Represents a function that accepts one argument and produces a result. * * <p>This is a <a href="package-summary.html">functional interface</a> * whose functional method is {@link #apply(Object)}. * * @param <T> the type of the input to the function * @param <R> the type of the result of the function * * @since 1.8 */ @FunctionalInterface public interface Function<T, R> { /** * Applies this function to the given argument. * * @param t the function argument * @return the function result */ R apply(T t); /** * Returns a composed function that first applies the {@code before} * function to its input, and then applies this function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <V> the type of input to the {@code before} function, and to the * composed function * @param before the function to apply before this function is applied * @return a composed function that first applies the {@code before} * function and then applies this function * @throws NullPointerException if before is null * * @see #andThen(Function) */ default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v)); } /** * Returns a composed function that first applies this function to * its input, and then applies the {@code after} function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <V> the type of output of the {@code after} function, and of the * composed function * @param after the function to apply after this function is applied * @return a composed function that first applies this function and then * applies the {@code after} function * @throws NullPointerException if after is null * * @see #compose(Function) */ default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); } /** * Returns a function that always returns its input argument. * * @param <T> the type of the input and output objects to the function * @return a function that always returns its input argument */ static <T> Function<T, T> identity() { return t -> t; } }
Function主要方法
apply方法
當(dāng)前方法就是我們使用匿名函數(shù)時(shí)需要重寫的方法,其中請求參數(shù)和返回參數(shù)都需要在我們生成Function對象的時(shí)候傳進(jìn)去,而apply方法也是這個(gè)類最核心的方法。
/** * Applies this function to the given argument. * * @param t the function argument * @return the function result */ R apply(T t);
創(chuàng)建 簡單的Function對象
Function是現(xiàn)有43個(gè)函數(shù)中基礎(chǔ)的函數(shù)之一,簡單的方法可以省略方法體{},和if的寫法一樣,但是復(fù)雜寫法不可以省略{}。
/** * ([參數(shù)列表]) ->{ * 代碼體; * } * 或 * ([參數(shù)列表]) ->代碼體 * */ public static void main(String[] args) { //Function<T, R> 傳入一個(gè)參數(shù),并返回一個(gè)參數(shù),兩個(gè)參數(shù)類型需要自己傳 可以對數(shù)據(jù)進(jìn)行處理 Function<String,Integer> stringIntegerAddFunction=(str)->Integer.parseInt(str)+1; System.out.println(stringIntegerAddFunction.apply("100")); Function<String,Integer> stringIntegerAddFunction2=(str)->{ Integer integer=Integer.parseInt(str); return integer+1; }; System.out.println(stringIntegerAddFunction2.apply("100")); }
兩個(gè)寫法雖然不一致,但是執(zhí)行的結(jié)果是一樣的。
compose方法、andThen方法和identity方法
使用匿名函數(shù)時(shí),匿名函數(shù)接口的類中只允許存在一個(gè)方法,而之所有這三方法,是因?yàn)榻涌谥锌梢酝ㄟ^關(guān)鍵字default定義默認(rèn)方法,實(shí)現(xiàn)類如果不想要默認(rèn)方法的實(shí)現(xiàn)邏輯可以根據(jù)需求重新定義,通過關(guān)鍵字static定義靜態(tài)方法,實(shí)現(xiàn)類如果不想要靜態(tài)方法的實(shí)現(xiàn)邏輯可以根據(jù)需求重新定義。
compose方法
有的時(shí)候,我們需要將兩個(gè)或多個(gè)方法進(jìn)行組合使用,這個(gè)時(shí)候就需要compose方法,compose會(huì)通過從右到左的順序執(zhí)行我們拼接的方法。
/** * ([參數(shù)列表]) ->{ * 代碼體; * } * 或 * ([參數(shù)列表]) ->代碼體 * */ public static void main(String[] args) { //Function<T, R> 傳入一個(gè)參數(shù),并返回一個(gè)參數(shù),兩個(gè)參數(shù)類型需要自己傳 可以對數(shù)據(jù)進(jìn)行處理 Function<String,Integer> stringIntegerAddFunction=(str)->Integer.parseInt(str)+1; Function<Integer,String> integerStringFunction=integer -> String.valueOf(integer*2); System.out.println(stringIntegerAddFunction.compose(integerStringFunction).apply(100)); }
可以通過結(jié)果看出,compose拼接會(huì)先執(zhí)行被拼接的函數(shù)(integerStringFunction),再回去調(diào)用我們拼接的函數(shù)(stringIntegerAddFunction),為方便查看,我們請求的apply方法的類型已經(jīng)發(fā)送了改變。
andThen方法
andThen方法則剛好相反,這個(gè)函數(shù)會(huì)把拼接的函數(shù)從左到右執(zhí)行。
/** * ([參數(shù)列表]) ->{ * 代碼體; * } * 或 * ([參數(shù)列表]) ->代碼體 * */ public static void main(String[] args) { //Function<T, R> 傳入一個(gè)參數(shù),并返回一個(gè)參數(shù),兩個(gè)參數(shù)類型需要自己傳 可以對數(shù)據(jù)進(jìn)行處理 Function<String,Integer> stringIntegerAddFunction=(str)->Integer.parseInt(str)+1; Function<Integer,String> integerStringFunction=integer -> String.valueOf(integer*2); System.out.println(stringIntegerAddFunction.andThen(integerStringFunction).apply("100")); }
根據(jù)結(jié)果可以看出,andThen拼接會(huì)先執(zhí)行被拼接的函數(shù)(stringIntegerAddFunction),再回去調(diào)用我們拼接的函數(shù)(integerStringFunction),為方便查看,我們請求的apply方法的類型已經(jīng)發(fā)送了改變。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解maven的setting配置文件中mirror和repository的區(qū)別
這篇文章主要介紹了詳解maven的setting配置文件中mirror和repository的區(qū)別,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12Java設(shè)計(jì)模式之橋接模式實(shí)例詳解
這篇文章主要介紹了Java設(shè)計(jì)模式之橋接模式,結(jié)合實(shí)例形式詳細(xì)分析了橋接模式的概念、功能、Java實(shí)現(xiàn)方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-09-09SpringBoot +Vue開發(fā)考試系統(tǒng)的教程
這篇文章主要介紹了SpringBoot +Vue開發(fā)考試系統(tǒng),支持多種題型:選擇題、多選題、判斷題、填空題、綜合題以及數(shù)學(xué)公式。支持在線考試,教師在線批改試卷。本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-05-05使用Springboot封裝好的發(fā)送post請求的工具類
本文介紹了在Springboot中封裝發(fā)送HTTP請求的工具類,并提供了普通的HTTP請求工具類代碼和Response類的使用示例,這些工具類可為開發(fā)者提供便利性和參考價(jià)值,幫助提高開發(fā)效率2024-09-09java實(shí)現(xiàn)翻轉(zhuǎn)單詞順序列
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)翻轉(zhuǎn)單詞順序列,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03mybaits-spring的實(shí)現(xiàn)方式
這篇文章主要介紹了mybaits-spring的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Java 入門圖形用戶界面設(shè)計(jì)之列表框JList
圖形界面(簡稱GUI)是指采用圖形方式顯示的計(jì)算機(jī)操作用戶界面。與早期計(jì)算機(jī)使用的命令行界面相比,圖形界面對于用戶來說在視覺上更易于接受,本篇精講Java語言中關(guān)于圖形用戶界面的列表框JList2022-02-02