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)文章
RxJava的消息發(fā)送和線程切換實(shí)現(xiàn)原理
這篇文章主要介紹了RxJava的消息發(fā)送和線程切換實(shí)現(xiàn)原理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11詳解Reactor如何優(yōu)雅Exception異常處理
初識(shí)響應(yīng)式編程的時(shí)候,除了從命令式的思維方式轉(zhuǎn)變?yōu)楹瘮?shù)式的編程方式外,其中有一個(gè)很大的不適應(yīng)的地方就是在面對(duì)異常時(shí)該怎么處理。本文將通過Project?Reactor的文檔以及源碼來深入解讀,在reactor中是如何優(yōu)雅地實(shí)現(xiàn)這異常處理三板斧,希望對(duì)大家有所幫助2023-02-02Java 實(shí)現(xiàn)一個(gè)漢諾塔實(shí)戰(zhàn)練習(xí)
漢諾塔是源于印度一個(gè)古老傳說的益智玩具。大梵天創(chuàng)造世界時(shí)做了三根石柱,在一根柱子上從下往上按大小順序摞著64片黃金圓盤。大梵天命令婆羅門把圓盤從下面開始按大小順序重新擺放在另一根柱子上。并且規(guī)定,在小圓盤上不能放大圓盤,三根柱子之間一次只能移動(dòng)一個(gè)圓盤2021-10-10詳解Java中格式化日期的DateFormat與SimpleDateFormat類
DateFormat其本身是一個(gè)抽象類,SimpleDateFormat 類是DateFormat類的子類,一般情況下來講DateFormat類很少會(huì)直接使用,而都使用SimpleDateFormat類完成,下面我們具體來看一下兩個(gè)類的用法:2016-05-05Netty網(wǎng)絡(luò)編程實(shí)戰(zhàn)之搭建Netty服務(wù)器
Netty是JBOSS開源的一款NIO網(wǎng)絡(luò)編程框架,可用于快速開發(fā)網(wǎng)絡(luò)的應(yīng)用。Netty是一個(gè)異步的、基于事件驅(qū)動(dòng)的網(wǎng)絡(luò)應(yīng)用框架,用于快速開發(fā)高性能的服務(wù)端和客戶端。本文將詳細(xì)說說如何搭建Netty服務(wù)器,需要的可以參考一下2022-10-10SpringBoot使用Thymeleaf自定義標(biāo)簽的實(shí)例代碼
這篇文章主要介紹了SpringBoot使用Thymeleaf自定義標(biāo)簽的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09SpringMVC與Mybatis集合實(shí)現(xiàn)調(diào)用存儲(chǔ)過程、事務(wù)控制實(shí)例
這篇文章主要介紹了SpringMVC與Mybatis集合實(shí)現(xiàn)調(diào)用存儲(chǔ)過程、事務(wù)控制實(shí)例,有需要的可以了解一下。2016-11-11