Java?8?中?Function?接口使用方法介紹
Java 8 中 Function 接口的介紹
Java 8 中提供了一個函數(shù)式接口 Function
,這個接口表示對一個參數(shù)做一些操作然后返回操作之后的值。這個接口的有一個抽象方法 apply
,這個方法就是表明對參數(shù)做的操作。
// Java Function 接口的定義 @FunctionalInterface public interface Function<T, R> { R apply(T t); default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v)); } default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); } static <T> Function<T, T> identity() { return t -> t; }
Function 接口的用法
apply
方法的使用方式如下:
Function<Integer, Integer> add = p -> p + 10; Integer result = add.apply(10); // 這里會輸出 20,因?yàn)檫@個函數(shù)定義的操作時把參數(shù)加上 10 后返回 System.out.println(result);
compose
方法的參數(shù)也是一個Function
對象。在 A 這個Function
上調(diào)用compose
方法時傳入 B 這個Function
對象,然后得到一個新的Function
對象 C。C 這個Function
對象的實(shí)現(xiàn)邏輯是先帶哦用 B 的apply
方法對參數(shù)進(jìn)行操作,將得到的結(jié)果再作為參數(shù)傳遞給 A 這個Function
對象的apply
法,然后返回執(zhí)行后的結(jié)果。
addThen
方法也是類似的原理,只不過內(nèi)部執(zhí)行方法的順序不一樣而已。
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));
Function 接口的實(shí)例
public class CheckUtils { private static final Function<String, String> lengthCheck = params -> { if (params.length() > 100) { throw new RuntimeException("Length exceed max limit."); } return params; }; private static final Function<String, String> invalidCharacterCheck = str -> { if (!str.matches("^[a-f,A-F]$")) { throw new RuntimeException("Contains invalid character."); } return str; }; /** * 這里的公共方法組合了該類中的基本校驗(yàn)邏輯構(gòu)成一個復(fù)合的邏輯 */ public static void checkStringLengthAndPhoneNumber(String string) { invalidCharacterCheck.compose(lengthCheck).apply(string); } }
到此這篇關(guān)于Java 8 中 Function 接口使用方法介紹的文章就介紹到這了,更多相關(guān)Java Function 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Boot?中的?@HystrixCommand?注解原理及使用方法
通過使用 @HystrixCommand 注解,我們可以輕松地實(shí)現(xiàn)對方法的隔離和監(jiān)控,從而提高系統(tǒng)的可靠性和穩(wěn)定性,本文介紹了Spring Boot 中的@HystrixCommand注解是什么,其原理以及如何使用,感興趣的朋友跟隨小編一起看看吧2023-07-07Java動態(tài)規(guī)劃之硬幣找零問題實(shí)現(xiàn)示例
本文主要介紹了Java動態(tài)規(guī)劃之硬幣找零問題實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Javaweb實(shí)現(xiàn)上傳下載文件的多種方法
本篇文章主要介紹了Javaweb實(shí)現(xiàn)上傳下載文件,有多種實(shí)現(xiàn)方式,需要的朋友可以參考下。2016-10-10TransmittableThreadLocal解決線程間上下文傳遞煩惱
這篇文章主要為大家介紹了TransmittableThreadLocal解決線程間上下文傳遞煩惱詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Springmvc如何返回xml及json格式數(shù)據(jù)
這篇文章主要介紹了Springmvc如何返回xml及json格式數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09Java Socket編程(三) 服務(wù)器Sockets
Java Socket編程(三) 服務(wù)器Sockets...2006-12-12