Java8中常見函數(shù)式接口的使用示例詳解
在 Java 8 中,函數(shù)式接口是一個關(guān)鍵的特性,它們允許將方法作為參數(shù)傳遞或返回類型,從而提高代碼的模塊性和靈活性。下面是一些常見的函數(shù)式接口的實例代碼。
1. Consumer<T>
Consumer<T> 接口代表一個接受單個輸入?yún)?shù)且不返回結(jié)果的操作。它常用于對對象執(zhí)行操作。
import java.util.function.Consumer;
Consumer<String> printConsumer = System.out::println;
printConsumer.accept("Hello, World!");
2. Supplier<T>
Supplier<T> 接口提供一個沒有參數(shù)的方法并返回一個泛型類型的結(jié)果。它通常用于延遲計算或構(gòu)造。
import java.util.function.Supplier; Supplier<Double> randomSupplier = Math::random; System.out.println(randomSupplier.get());
3. Function<T,R>
Function<T,R> 接口表示接受一個參數(shù)并產(chǎn)生結(jié)果的函數(shù)。這是一個非常通用的接口。
import java.util.function.Function;
Function<String, Integer> lengthFunction = String::length;
System.out.println(lengthFunction.apply("Hello"));
4. Predicate<T>
Predicate<T> 接口表示一個參數(shù)的布爾值函數(shù)。
import java.util.function.Predicate;
Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty();
System.out.println(nonEmptyStringPredicate.test("Test"));
5.使用 Function<T,R> 進行函數(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> 進行邏輯組合
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> 的一個特例,用于操作單個操作數(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> 接口表示接受兩個參數(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> 的一個特例,用于操作兩個相同類型的操作數(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> 接口表示接受兩個輸入?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> 接口表示接受兩個參數(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> 接口表示將一個對象轉(zhuǎn)換為一個原始 int 類型的函數(shù)。
import java.util.function.ToIntFunction;
ToIntFunction<String> length = String::length;
System.out.println(length.applyAsInt("Hello")); // 輸出 5
通過這些特性,Java 8 的函數(shù)式接口極大地提升了代碼的簡潔性和可讀性,同時也促進了函數(shù)式編程范式在 Java 社區(qū)中的普及。
到此這篇關(guān)于Java8中常見函數(shù)式接口的使用示例詳解的文章就介紹到這了,更多相關(guān)Java8函數(shù)式接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何解決zookeeper集群重啟Error contacting service.It
本文詳細(xì)介紹了Zookeeper集群啟動異常的排查步驟,包括網(wǎng)絡(luò)問題、防火墻配置、Java環(huán)境、端口占用、網(wǎng)卡問題、網(wǎng)絡(luò)問題以及節(jié)點配置信息的檢查,通過逐一排查和解決這些問題,可以有效地啟動Zookeeper集群2024-12-12
SpringCloud2020 bootstrap 配置文件失效的解決方法
這篇文章主要介紹了SpringCloud2020 bootstrap 配置文件失效的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
簡單了解Thymeleaf語法 數(shù)據(jù)延遲加載使用實例
這篇文章主要介紹了簡單了解Thymeleaf語法 數(shù)據(jù)延遲加載使用實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2010-05-05
基于SpringBoot+Redis的Session共享與單點登錄詳解
這篇文章主要介紹了基于SpringBoot+Redis的Session共享與單點登錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07
Java操作redis實現(xiàn)增刪查改功能的方法示例
這篇文章主要介紹了Java操作redis實現(xiàn)增刪查改功能的方法,涉及java操作redis數(shù)據(jù)庫的連接、設(shè)置、增刪改查、釋放資源等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
spring cloud gateway如何獲取請求的真實地址
這篇文章主要介紹了spring cloud gateway如何獲取請求的真實地址問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05

