欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中Function的使用及說明

 更新時間:2023年05月31日 09:33:38   作者:華妃  
這篇文章主要介紹了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é)果是一樣的。

簡單Funciton對象的返回結(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ā)送了改變。

使用compose方法返回結(jié)果

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ā)送了改變。

andThen方法返回結(jié)果

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解maven的setting配置文件中mirror和repository的區(qū)別

    詳解maven的setting配置文件中mirror和repository的區(qū)別

    這篇文章主要介紹了詳解maven的setting配置文件中mirror和repository的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • HttpClient詳細使用示例代碼

    HttpClient詳細使用示例代碼

    這篇文章主要介紹了HttpClient詳細使用示例,包括導入依賴,使用工具類的詳細代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • Java設計模式之橋接模式實例詳解

    Java設計模式之橋接模式實例詳解

    這篇文章主要介紹了Java設計模式之橋接模式,結(jié)合實例形式詳細分析了橋接模式的概念、功能、Java實現(xiàn)方法及相關(guān)注意事項,需要的朋友可以參考下
    2017-09-09
  • SpringBoot +Vue開發(fā)考試系統(tǒng)的教程

    SpringBoot +Vue開發(fā)考試系統(tǒng)的教程

    這篇文章主要介紹了SpringBoot +Vue開發(fā)考試系統(tǒng),支持多種題型:選擇題、多選題、判斷題、填空題、綜合題以及數(shù)學公式。支持在線考試,教師在線批改試卷。本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2020-05-05
  • 使用Springboot封裝好的發(fā)送post請求的工具類

    使用Springboot封裝好的發(fā)送post請求的工具類

    本文介紹了在Springboot中封裝發(fā)送HTTP請求的工具類,并提供了普通的HTTP請求工具類代碼和Response類的使用示例,這些工具類可為開發(fā)者提供便利性和參考價值,幫助提高開發(fā)效率
    2024-09-09
  • java實現(xiàn)翻轉(zhuǎn)單詞順序列

    java實現(xiàn)翻轉(zhuǎn)單詞順序列

    這篇文章主要為大家詳細介紹了java實現(xiàn)翻轉(zhuǎn)單詞順序列,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • mybaits-spring的實現(xiàn)方式

    mybaits-spring的實現(xiàn)方式

    這篇文章主要介紹了mybaits-spring的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java 入門圖形用戶界面設計之列表框JList

    Java 入門圖形用戶界面設計之列表框JList

    圖形界面(簡稱GUI)是指采用圖形方式顯示的計算機操作用戶界面。與早期計算機使用的命令行界面相比,圖形界面對于用戶來說在視覺上更易于接受,本篇精講Java語言中關(guān)于圖形用戶界面的列表框JList
    2022-02-02
  • Spring攔截器和過濾器的區(qū)別在哪?

    Spring攔截器和過濾器的區(qū)別在哪?

    相信很多小伙伴都對Spring攔截器和過濾器的區(qū)別有疑惑,今天特地整理了本篇文章,文中有非常詳細的介紹,需要的朋友可以參考下
    2021-06-06
  • Java中Sentinel框架詳解

    Java中Sentinel框架詳解

    Sentinel是一個高可用、高擴展、高穩(wěn)定性的開源流量控制和熔斷降級框架,可以在分布式系統(tǒng)中實現(xiàn)實時的流量控制,防止系統(tǒng)因流量過大導致系統(tǒng)崩潰和服務降級,Sentinel面向所有的Java應用,本文就給大家詳細介紹一下Java中Sentinel框架,需要的朋友可以參考下
    2023-06-06

最新評論