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接口只有一個(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ì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用filter實(shí)現(xiàn)url級(jí)別內(nèi)存緩存示例
這篇文章主要介紹了使用filter實(shí)現(xiàn)url級(jí)別內(nèi)存緩存示例,只需要一個(gè)靜態(tài)類,在filter中調(diào)用,也可以全部寫到filt里面。可以根據(jù)查詢參數(shù)分別緩存,需要的朋友可以參考下2014-03-03
關(guān)于Unsupported Media Type的解決方案
在Web開發(fā)中,415錯(cuò)誤表示服務(wù)器無法處理請(qǐng)求附帶的媒體格式,本文介紹了導(dǎo)致HTTP 415錯(cuò)誤的原因以及解決該問題的兩種方法,首先,415錯(cuò)誤通常是由于客戶端請(qǐng)求的內(nèi)容類型與服務(wù)器期望的不匹配引起的,例如,服務(wù)器可能期望JSON格式的數(shù)據(jù)2024-10-10
java8中的Collectors.groupingBy用法詳解
這篇文章主要介紹了java8中的Collectors.groupingBy用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Java?代碼本地設(shè)置Hadoop用戶名密碼的方法
在Hadoop環(huán)境中,通常使用Kerberos進(jìn)行身份驗(yàn)證,這篇文章主要介紹了Java?代碼本地設(shè)置Hadoop用戶名密碼的方法,需要的朋友可以參考下2024-08-08
詳解Spring Cloud中Hystrix的請(qǐng)求合并
這篇文章主要介紹了詳解Spring Cloud中Hystrix的請(qǐng)求合并,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
java:程序包c(diǎn)om.xxx.xxx不存在報(bào)錯(cuò)萬能解決辦法
這篇文章主要給大家介紹了關(guān)于java:程序包c(diǎn)om.xxx.xxx不存在報(bào)錯(cuò)萬能解決辦法,這個(gè)問題曾逼瘋初學(xué)者的我,不過弄清楚原理后就很簡單了,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12

