這篇文章主要講解了Java8 Supplier接口和Consumer接口的用法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

創(chuàng)新互聯(lián)建站于2013年開始,我們提供高端網(wǎng)站建設(shè)、成都小程序開發(fā)、電商視覺設(shè)計、APP應(yīng)用開發(fā)及網(wǎng)絡(luò)營銷搜索優(yōu)化服務(wù),在傳統(tǒng)互聯(lián)網(wǎng)與移動互聯(lián)網(wǎng)發(fā)展的背景下,我們堅守著用標(biāo)準(zhǔn)的設(shè)計方案與技術(shù)開發(fā)實力作基礎(chǔ),以企業(yè)及品牌的互聯(lián)網(wǎng)商業(yè)目標(biāo)為核心,為客戶打造具商業(yè)價值與用戶體驗的互聯(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接口只有一個抽象方法get(),通過get方法產(chǎn)生一個T類型實例。
實例:
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)建實例");
}
}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); };
}
}一個抽象方法accept(T t)定義了要執(zhí)行的具體操作;注意看andThen方法,接收Consumer<? super T>類型參數(shù),返回一個lambda表達式,此表達式定義了新的執(zhí)行過程,先執(zhí)行當(dāng)前Consumer實例的accept方法,再執(zhí)行入?yún)鬟M來的Consumer實例的accept方法,這兩個accept方法接收都是相同的入?yún)。
實例:
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)容,是不是對Java8 Supplier接口和Consumer接口的用法有進一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站標(biāo)題:Java8Supplier接口和Consumer接口的用法
分享地址:http://www.chinadenli.net/article18/gghpdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、動態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計、ChatGPT、服務(wù)器托管、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)