最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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ù)應(yīng)用于給定的參數(shù)。

default Function<V, R> compose(Function<? super V, ? extends T> before)

返回一個組合函數(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)

返回一個組合函數(shù),首先將此函數(shù)應(yīng)用于其輸入,然后將after函數(shù)應(yīng)用于結(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方法

當(dāng)前方法就是我們使用匿名函數(shù)時需要重寫的方法,其中請求參數(shù)和返回參數(shù)都需要在我們生成Function對象的時候傳進(jìn)去,而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ù)中基礎(chǔ)的函數(shù)之一,簡單的方法可以省略方法體{},和if的寫法一樣,但是復(fù)雜寫法不可以省略{}。

/**
	 * ([參數(shù)列表]) ->{
 	 * 	代碼體;
 	 * }
 	 * 或
	 * ([參數(shù)列表]) ->代碼體
	 *
	 */
    public static void main(String[] args) {
        //Function<T, R> 傳入一個參數(shù),并返回一個參數(shù),兩個參數(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"));
    }

兩個寫法雖然不一致,但是執(zhí)行的結(jié)果是一樣的。

簡單Funciton對象的返回結(jié)果

compose方法、andThen方法和identity方法

使用匿名函數(shù)時,匿名函數(shù)接口的類中只允許存在一個方法,而之所有這三方法,是因為接口中可以通過關(guān)鍵字default定義默認(rèn)方法,實現(xiàn)類如果不想要默認(rèn)方法的實現(xiàn)邏輯可以根據(jù)需求重新定義,通過關(guān)鍵字static定義靜態(tài)方法,實現(xiàn)類如果不想要靜態(tài)方法的實現(xiàn)邏輯可以根據(jù)需求重新定義。

compose方法

有的時候,我們需要將兩個或多個方法進(jìn)行組合使用,這個時候就需要compose方法,compose會通過從右到左的順序執(zhí)行我們拼接的方法。

/**
	 * ([參數(shù)列表]) ->{
 	 * 	代碼體;
 	 * }
 	 * 或
	 * ([參數(shù)列表]) ->代碼體
	 *
	 */
 public static void main(String[] args) {
        //Function<T, R> 傳入一個參數(shù),并返回一個參數(shù),兩個參數(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拼接會先執(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ù)進(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拼接會先執(zhí)行被拼接的函數(shù)(stringIntegerAddFunction),再回去調(diào)用我們拼接的函數(shù)(integerStringFunction),為方便查看,我們請求的apply方法的類型已經(jīng)發(fā)送了改變。

andThen方法返回結(jié)果

總結(jié)

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

相關(guān)文章

  • 詳解JAVA Stream流

    詳解JAVA Stream流

    這篇文章主要介紹了JAVA Stream流的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • java對象轉(zhuǎn)化成String類型的四種方法小結(jié)

    java對象轉(zhuǎn)化成String類型的四種方法小結(jié)

    在java項目的實際開發(fā)和應(yīng)用中,常常需要用到將對象轉(zhuǎn)為String這一基本功能。本文就詳細(xì)的介紹幾種方法,感興趣的可以了解一下
    2021-08-08
  • IDEA 配置 JRebel 熱部署的方法(推薦)

    IDEA 配置 JRebel 熱部署的方法(推薦)

    這篇文章主要介紹了IDEA 配置 JRebel 熱部署的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • 深入理解Maven的坐標(biāo)與依賴

    深入理解Maven的坐標(biāo)與依賴

    這篇文章主要介紹了深入理解Maven的坐標(biāo)與依賴,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • Java數(shù)據(jù)結(jié)構(gòu)之順序表的實現(xiàn)

    Java數(shù)據(jù)結(jié)構(gòu)之順序表的實現(xiàn)

    線性表(linear?list)是n個具有相同特性的數(shù)據(jù)元素的有限序列。順序表是常見的線性表之一,本文將詳細(xì)講講順序表的原理與實現(xiàn),需要的可以參考一下
    2022-08-08
  • idea如何將指定目錄打成jar包

    idea如何將指定目錄打成jar包

    這篇文章主要介紹了idea如何將指定目錄打成jar包問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • SpringBoot整合ES解析搜索返回字段問題

    SpringBoot整合ES解析搜索返回字段問題

    這篇文章主要介紹了SpringBoot整合ES解析搜索返回字段問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • java利用SMB讀取遠(yuǎn)程文件的方法

    java利用SMB讀取遠(yuǎn)程文件的方法

    這篇文章主要為大家詳細(xì)介紹了java利用SMB讀取遠(yuǎn)程文件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Java對象轉(zhuǎn)換的實現(xiàn)方式匯總

    Java對象轉(zhuǎn)換的實現(xiàn)方式匯總

    這篇文章主要介紹了Java對象轉(zhuǎn)換的多種實現(xiàn)方式,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2025-04-04
  • Springboot @Transactional大事務(wù)處理的幾點建議

    Springboot @Transactional大事務(wù)處理的幾點建議

    本文主要介紹了大事務(wù)的概念及其危害,并提出了幾種解決大事務(wù)問題的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01

最新評論

延吉市| 汽车| 印江| 五华县| 佛坪县| 泾川县| 石泉县| 仁怀市| 婺源县| 静乐县| 辽阳县| 论坛| 浪卡子县| 阿拉善右旗| 银川市| 仙桃市| 南投市| 郑州市| 贵州省| 邯郸县| 宁德市| 旬邑县| 井陉县| 崇明县| 聂荣县| 和田市| 乐都县| 玛曲县| 扎赉特旗| 五莲县| 罗江县| 红河县| 三原县| 金昌市| 永平县| 思南县| 射阳县| 克拉玛依市| 青川县| 洛隆县| 景谷|