Java8中常見函數(shù)式接口的使用示例詳解
在 Java 8 中,函數(shù)式接口是一個(gè)關(guān)鍵的特性,它們?cè)试S將方法作為參數(shù)傳遞或返回類型,從而提高代碼的模塊性和靈活性。下面是一些常見的函數(shù)式接口的實(shí)例代碼。
1. Consumer<T>
Consumer<T> 接口代表一個(gè)接受單個(gè)輸入?yún)?shù)且不返回結(jié)果的操作。它常用于對(duì)對(duì)象執(zhí)行操作。
import java.util.function.Consumer; Consumer<String> printConsumer = System.out::println; printConsumer.accept("Hello, World!");
2. Supplier<T>
Supplier<T> 接口提供一個(gè)沒有參數(shù)的方法并返回一個(gè)泛型類型的結(jié)果。它通常用于延遲計(jì)算或構(gòu)造。
import java.util.function.Supplier; Supplier<Double> randomSupplier = Math::random; System.out.println(randomSupplier.get());
3. Function<T,R>
Function<T,R> 接口表示接受一個(gè)參數(shù)并產(chǎn)生結(jié)果的函數(shù)。這是一個(gè)非常通用的接口。
import java.util.function.Function; Function<String, Integer> lengthFunction = String::length; System.out.println(lengthFunction.apply("Hello"));
4. Predicate<T>
Predicate<T> 接口表示一個(gè)參數(shù)的布爾值函數(shù)。
import java.util.function.Predicate; Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty(); System.out.println(nonEmptyStringPredicate.test("Test"));
5.使用 Function<T,R> 進(jìn)行函數(shù)組合
Function<Integer, Integer> times2 = e -> e * 2; Function<Integer, Integer> squared = e -> e * e; Function<Integer, Integer> times2AndSquare = times2.andThen(squared); System.out.println(times2AndSquare.apply(4)); // 輸出 64
6.使用 Predicate<T> 進(jìn)行邏輯組合
Predicate<Integer> isEven = x -> x % 2 == 0; Predicate<Integer> isPositive = x -> x > 0; Predicate<Integer> isEvenAndPositive = isEven.and(isPositive); System.out.println(isEvenAndPositive.test(4)); // true
7. UnaryOperator<T>
UnaryOperator<T> 是 Function<T, T> 的一個(gè)特例,用于操作單個(gè)操作數(shù),其類型與結(jié)果類型相同。
import java.util.function.UnaryOperator; UnaryOperator<Integer> square = x -> x * x; System.out.println(square.apply(5)); // 輸出 25
8. BiFunction<T, U, R>
BiFunction<T, U, R> 接口表示接受兩個(gè)參數(shù)并產(chǎn)生結(jié)果的函數(shù)。
import java.util.function.BiFunction; BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b; System.out.println(add.apply(2, 3)); // 輸出 5
9. BinaryOperator<T>
BinaryOperator<T> 是 BiFunction<T, T, T> 的一個(gè)特例,用于操作兩個(gè)相同類型的操作數(shù)并返回相同類型的結(jié)果。
import java.util.function.BinaryOperator; BinaryOperator<Integer> multiply = (a, b) -> a * b; System.out.println(multiply.apply(3, 4)); // 輸出 12
10.使用 Function<T, R> 創(chuàng)建工廠方法
import java.util.HashMap; import java.util.Map; import java.util.function.Function; class ComplexClass { private int value; ComplexClass(int value) { this.value = value; } // Getter and other methods... } Function<Integer, ComplexClass> complexClassFactory = ComplexClass::new; ComplexClass complexInstance = complexClassFactory.apply(10);
11.UnaryOperator<T> 創(chuàng)建連續(xù)操作
UnaryOperator<String> toUpperCase = String::toUpperCase; UnaryOperator<String> addExclamation = str -> str + "!"; UnaryOperator<String> shout = toUpperCase.andThen(addExclamation); System.out.println(shout.apply("hello")); // 輸出 "HELLO!"
12. BiConsumer<T, U>
BiConsumer<T, U> 接口表示接受兩個(gè)輸入?yún)?shù)的操作,并且不返回任何結(jié)果。
import java.util.function.BiConsumer; BiConsumer<String, String> concatAndPrint = (a, b) -> System.out.println(a + b); concatAndPrint.accept("Hello, ", "World!"); // 輸出 Hello, World!
13. BiPredicate<T, U>
BiPredicate<T, U> 接口表示接受兩個(gè)參數(shù)的布爾值函數(shù)。
import java.util.function.BiPredicate; BiPredicate<Integer, String> validate = (i, s) -> i > 0 && s.startsWith("A"); System.out.println(validate.test(1, "Apple")); // 輸出 true
14. ToIntFunction<T>
ToIntFunction<T> 接口表示將一個(gè)對(duì)象轉(zhuǎn)換為一個(gè)原始 int 類型的函數(shù)。
import java.util.function.ToIntFunction; ToIntFunction<String> length = String::length; System.out.println(length.applyAsInt("Hello")); // 輸出 5
通過這些特性,Java 8 的函數(shù)式接口極大地提升了代碼的簡(jiǎn)潔性和可讀性,同時(shí)也促進(jìn)了函數(shù)式編程范式在 Java 社區(qū)中的普及。
到此這篇關(guān)于Java8中常見函數(shù)式接口的使用示例詳解的文章就介紹到這了,更多相關(guān)Java8函數(shù)式接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis-Flex實(shí)現(xiàn)多表聯(lián)查(自動(dòng)映射)
我們可以輕松的使用 Mybaits-Flex 鏈接任何數(shù)據(jù)庫(kù),本文主要介紹了MyBatis-Flex實(shí)現(xiàn)多表聯(lián)查(自動(dòng)映射),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06@PathVariable和@RequestParam傳參為空問題及解決
這篇文章主要介紹了@PathVariable和@RequestParam傳參為空問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Kotlin?標(biāo)準(zhǔn)函數(shù)和靜態(tài)方法示例詳解
這篇文章主要為大家介紹了Kotlin?標(biāo)準(zhǔn)函數(shù)和靜態(tài)方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10IntelliJ IDEA(2020.2)的下載、安裝步驟詳細(xì)教程
這篇文章主要介紹了IntelliJ IDEA(2020.2)的下載、安裝步驟詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Java實(shí)現(xiàn)解數(shù)獨(dú)的小程序
最近在學(xué)習(xí)Java,然后上個(gè)月迷上了九宮格數(shù)獨(dú),玩了幾天,覺得實(shí)在有趣,就想著能不能用編程來解決,于是就自己寫了個(gè),還真解決了。下面這篇文章就給大家主要介紹了Java實(shí)現(xiàn)解數(shù)獨(dú)的小程序,需要的朋友可以參考借鑒。2017-01-01java 多線程的幾種實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了java 多線程的幾種實(shí)現(xiàn)方法總結(jié)的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握java多線程的知識(shí),需要的朋友可以參考下2017-10-10