這篇文章主要講解了Java8 Supplier接口和Consumer接口的用法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
創(chuàng)新互聯(lián)建站于2013年開始,我們提供高端網(wǎng)站建設(shè)、成都小程序開發(fā)、電商視覺設(shè)計(jì)、APP應(yīng)用開發(fā)及網(wǎng)絡(luò)營銷搜索優(yōu)化服務(wù),在傳統(tǒng)互聯(lián)網(wǎng)與移動(dòng)互聯(lián)網(wǎng)發(fā)展的背景下,我們堅(jiān)守著用標(biāo)準(zhǔn)的設(shè)計(jì)方案與技術(shù)開發(fā)實(shí)力作基礎(chǔ),以企業(yè)及品牌的互聯(lián)網(wǎng)商業(yè)目標(biāo)為核心,為客戶打造具商業(yè)價(jià)值與用戶體驗(yàn)的互聯(lián)網(wǎng)+產(chǎn)品。
Supplier接口
package java.util.function; /** * Represents a supplier of results. * * <p>There is no requirement that a new or distinct result be returned each * time the supplier is invoked. * * <p>This is a <a href="package-summary.html" rel="external nofollow" rel="external nofollow" >functional interface</a> * whose functional method is {@link #get()}. * * @param <T> the type of results supplied by this supplier * * @since 1.8 */ @FunctionalInterface public interface Supplier<T> { /** * Gets a result. * * @return a result */ T get(); }
supplier接口只有一個(gè)抽象方法get(),通過get方法產(chǎn)生一個(gè)T類型實(shí)例。
實(shí)例:
package me.yanand; import java.util.function.Supplier; public class TestSupplier { public static void main(String[] args) { Supplier<Apple> appleSupplier = Apple::new; System.out.println("--------"); appleSupplier.get(); } } class Apple{ public Apple() { System.out.println("創(chuàng)建實(shí)例"); } }
Consumer接口
package java.util.function; import java.util.Objects; /** * Represents an operation that accepts a single input argument and returns no * result. Unlike most other functional interfaces, {@code Consumer} is expected * to operate via side-effects. * * <p>This is a <a href="package-summary.html" rel="external nofollow" rel="external nofollow" >functional interface</a> * whose functional method is {@link #accept(Object)}. * * @param <T> the type of the input to the operation * * @since 1.8 */ @FunctionalInterface public interface Consumer<T> { /** * Performs this operation on the given argument. * * @param t the input argument */ void accept(T t); /** * Returns a composed {@code Consumer} that performs, in sequence, this * operation followed by the {@code after} operation. If performing either * operation throws an exception, it is relayed to the caller of the * composed operation. If performing this operation throws an exception, * the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * @return a composed {@code Consumer} that performs in sequence this * operation followed by the {@code after} operation * @throws NullPointerException if {@code after} is null */ default Consumer<T> andThen(Consumer<? super T> after) { Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(t); }; } }
一個(gè)抽象方法accept(T t)定義了要執(zhí)行的具體操作;注意看andThen方法,接收Consumer<? super T>類型參數(shù),返回一個(gè)lambda表達(dá)式,此表達(dá)式定義了新的執(zhí)行過程,先執(zhí)行當(dāng)前Consumer實(shí)例的accept方法,再執(zhí)行入?yún)鬟M(jìn)來的Consumer實(shí)例的accept方法,這兩個(gè)accept方法接收都是相同的入?yún)。
實(shí)例:
package me.yanand; import java.util.function.Consumer; public class TestConsumer { public static void main(String[] args) { Consumer<Integer> consumer = (t) -> { System.out.println(t*3); }; Consumer<Integer> consumerAfter = (s) -> { System.out.println("之后執(zhí)行:"+s); }; consumer.andThen(consumerAfter).accept(5); } }
看完上述內(nèi)容,是不是對(duì)Java8 Supplier接口和Consumer接口的用法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站標(biāo)題:Java8Supplier接口和Consumer接口的用法
分享地址:http://aaarwkj.com/article18/gghpdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、ChatGPT、服務(wù)器托管、網(wǎng)頁設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)