Java中Function的使用及說明
Java Function的使用
一、方法介紹
表示接受一個參數(shù)并產(chǎn)生結(jié)果的函數(shù)。
參數(shù)類型
- T - 函數(shù)輸入的類型
- R - 函數(shù)的結(jié)果類型
方法介紹
R apply(T t)
將此函數(shù)應用于給定的參數(shù)。
default Function<V, R> compose(Function<? super V, ? extends T> before)
返回一個組合函數(shù),首先將before函數(shù)應用于其輸入,然后將此函數(shù)應用于結(jié)果。 如果任一函數(shù)的評估引發(fā)異常,則將其轉(zhuǎn)發(fā)給組合函數(shù)的調(diào)用者。
default Function<T, V> andThen(Function<? super R, ? extends V> after)
返回一個組合函數(shù),首先將此函數(shù)應用于其輸入,然后將after函數(shù)應用于結(jié)果。 如果任一函數(shù)的評估引發(fā)異常,則將其轉(zhuǎn)發(fā)給組合函數(shù)的調(diào)用者。
static Function<T, T> identity()
返回一個總是返回其輸入?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); // 這里會輸出 20,因為這個函數(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 包中,其方法主要用于對一個請求參數(shù)的處理,并返回一個結(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方法
當前方法就是我們使用匿名函數(shù)時需要重寫的方法,其中請求參數(shù)和返回參數(shù)都需要在我們生成Function對象的時候傳進去,而apply方法也是這個類最核心的方法。
/** * 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個函數(shù)中基礎的函數(shù)之一,簡單的方法可以省略方法體{},和if的寫法一樣,但是復雜寫法不可以省略{}。
/** * ([參數(shù)列表]) ->{ * 代碼體; * } * 或 * ([參數(shù)列表]) ->代碼體 * */ public static void main(String[] args) { //Function<T, R> 傳入一個參數(shù),并返回一個參數(shù),兩個參數(shù)類型需要自己傳 可以對數(shù)據(jù)進行處理 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")); }
兩個寫法雖然不一致,但是執(zhí)行的結(jié)果是一樣的。
compose方法、andThen方法和identity方法
使用匿名函數(shù)時,匿名函數(shù)接口的類中只允許存在一個方法,而之所有這三方法,是因為接口中可以通過關(guān)鍵字default定義默認方法,實現(xiàn)類如果不想要默認方法的實現(xiàn)邏輯可以根據(jù)需求重新定義,通過關(guān)鍵字static定義靜態(tài)方法,實現(xiàn)類如果不想要靜態(tài)方法的實現(xiàn)邏輯可以根據(jù)需求重新定義。
compose方法
有的時候,我們需要將兩個或多個方法進行組合使用,這個時候就需要compose方法,compose會通過從右到左的順序執(zhí)行我們拼接的方法。
/** * ([參數(shù)列表]) ->{ * 代碼體; * } * 或 * ([參數(shù)列表]) ->代碼體 * */ public static void main(String[] args) { //Function<T, R> 傳入一個參數(shù),并返回一個參數(shù),兩個參數(shù)類型需要自己傳 可以對數(shù)據(jù)進行處理 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拼接會先執(zhí)行被拼接的函數(shù)(integerStringFunction),再回去調(diào)用我們拼接的函數(shù)(stringIntegerAddFunction),為方便查看,我們請求的apply方法的類型已經(jīng)發(fā)送了改變。
andThen方法
andThen方法則剛好相反,這個函數(shù)會把拼接的函數(shù)從左到右執(zhí)行。
/** * ([參數(shù)列表]) ->{ * 代碼體; * } * 或 * ([參數(shù)列表]) ->代碼體 * */ public static void main(String[] args) { //Function<T, R> 傳入一個參數(shù),并返回一個參數(shù),兩個參數(shù)類型需要自己傳 可以對數(shù)據(jù)進行處理 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拼接會先執(zhí)行被拼接的函數(shù)(stringIntegerAddFunction),再回去調(diào)用我們拼接的函數(shù)(integerStringFunction),為方便查看,我們請求的apply方法的類型已經(jīng)發(fā)送了改變。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解maven的setting配置文件中mirror和repository的區(qū)別
這篇文章主要介紹了詳解maven的setting配置文件中mirror和repository的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12SpringBoot +Vue開發(fā)考試系統(tǒng)的教程
這篇文章主要介紹了SpringBoot +Vue開發(fā)考試系統(tǒng),支持多種題型:選擇題、多選題、判斷題、填空題、綜合題以及數(shù)學公式。支持在線考試,教師在線批改試卷。本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2020-05-05使用Springboot封裝好的發(fā)送post請求的工具類
本文介紹了在Springboot中封裝發(fā)送HTTP請求的工具類,并提供了普通的HTTP請求工具類代碼和Response類的使用示例,這些工具類可為開發(fā)者提供便利性和參考價值,幫助提高開發(fā)效率2024-09-09