Java8 Supplier接口和Consumer接口原理解析
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í)行當前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); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
在windows環(huán)境下安裝jdk8、jdk9、jdk11、jdk12并自由切換
這篇文章主要介紹了在windows環(huán)境下安裝jdk8、jdk9、jdk11、jdk12并自由切換,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05使用java swing實現(xiàn)qq登錄界面示例分享
這篇文章主要介紹了使用java swing實現(xiàn)qq登錄界面示例,需要的朋友可以參考下2014-04-04如何使用SpringBootCondition更自由地定義條件化配置
這篇文章主要介紹了如何使用SpringBootCondition更自由地定義條件化配置,幫助大家更好的理解和學習使用springboot框架,感興趣的朋友可以了解下2021-04-04Spring Cloud Ubuntu環(huán)境部署的步驟與注意事項
這篇文章主要給大家介紹了關于Spring Cloud Ubuntu環(huán)境部署的步驟與注意事項,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Cloud具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12