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)文章
RxJava的消息發(fā)送和線程切換實現(xiàn)原理
這篇文章主要介紹了RxJava的消息發(fā)送和線程切換實現(xiàn)原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11詳解Reactor如何優(yōu)雅Exception異常處理
初識響應式編程的時候,除了從命令式的思維方式轉(zhuǎn)變?yōu)楹瘮?shù)式的編程方式外,其中有一個很大的不適應的地方就是在面對異常時該怎么處理。本文將通過Project?Reactor的文檔以及源碼來深入解讀,在reactor中是如何優(yōu)雅地實現(xiàn)這異常處理三板斧,希望對大家有所幫助2023-02-02詳解Java中格式化日期的DateFormat與SimpleDateFormat類
DateFormat其本身是一個抽象類,SimpleDateFormat 類是DateFormat類的子類,一般情況下來講DateFormat類很少會直接使用,而都使用SimpleDateFormat類完成,下面我們具體來看一下兩個類的用法:2016-05-05Netty網(wǎng)絡(luò)編程實戰(zhàn)之搭建Netty服務(wù)器
Netty是JBOSS開源的一款NIO網(wǎng)絡(luò)編程框架,可用于快速開發(fā)網(wǎng)絡(luò)的應用。Netty是一個異步的、基于事件驅(qū)動的網(wǎng)絡(luò)應用框架,用于快速開發(fā)高性能的服務(wù)端和客戶端。本文將詳細說說如何搭建Netty服務(wù)器,需要的可以參考一下2022-10-10SpringBoot使用Thymeleaf自定義標簽的實例代碼
這篇文章主要介紹了SpringBoot使用Thymeleaf自定義標簽的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09SpringMVC與Mybatis集合實現(xiàn)調(diào)用存儲過程、事務(wù)控制實例
這篇文章主要介紹了SpringMVC與Mybatis集合實現(xiàn)調(diào)用存儲過程、事務(wù)控制實例,有需要的可以了解一下。2016-11-11