最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java8 Supplier接口和Consumer接口原理解析

 更新時間:2020年04月28日 14:28:37   作者:Terry  
這篇文章主要介紹了Java8 Supplier接口和Consumer接口原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

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表達(dá)式,此表達(dá)式定義了新的執(zhí)行過程,先執(zhí)行當(dāng)前Consumer實例的accept方法,再執(zhí)行入?yún)鬟M(jìn)來的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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用filter實現(xiàn)url級別內(nèi)存緩存示例

    使用filter實現(xiàn)url級別內(nèi)存緩存示例

    這篇文章主要介紹了使用filter實現(xiàn)url級別內(nèi)存緩存示例,只需要一個靜態(tài)類,在filter中調(diào)用,也可以全部寫到filt里面??梢愿鶕?jù)查詢參數(shù)分別緩存,需要的朋友可以參考下
    2014-03-03
  • spring Roo安裝使用簡介

    spring Roo安裝使用簡介

    這篇文章主要介紹了spring Roo安裝使用簡介,具有一定借鑒價值,需要的朋友可以參考下
    2017-12-12
  • 關(guān)于Unsupported Media Type的解決方案

    關(guān)于Unsupported Media Type的解決方案

    在Web開發(fā)中,415錯誤表示服務(wù)器無法處理請求附帶的媒體格式,本文介紹了導(dǎo)致HTTP 415錯誤的原因以及解決該問題的兩種方法,首先,415錯誤通常是由于客戶端請求的內(nèi)容類型與服務(wù)器期望的不匹配引起的,例如,服務(wù)器可能期望JSON格式的數(shù)據(jù)
    2024-10-10
  • java8中的Collectors.groupingBy用法詳解

    java8中的Collectors.groupingBy用法詳解

    這篇文章主要介紹了java8中的Collectors.groupingBy用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • java獲取反射機(jī)制的3種方法總結(jié)

    java獲取反射機(jī)制的3種方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于java獲取反射機(jī)制的3種方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Java?代碼本地設(shè)置Hadoop用戶名密碼的方法

    Java?代碼本地設(shè)置Hadoop用戶名密碼的方法

    在Hadoop環(huán)境中,通常使用Kerberos進(jìn)行身份驗證,這篇文章主要介紹了Java?代碼本地設(shè)置Hadoop用戶名密碼的方法,需要的朋友可以參考下
    2024-08-08
  • 詳解Spring Cloud中Hystrix的請求合并

    詳解Spring Cloud中Hystrix的請求合并

    這篇文章主要介紹了詳解Spring Cloud中Hystrix的請求合并,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • java:程序包com.xxx.xxx不存在報錯萬能解決辦法

    java:程序包com.xxx.xxx不存在報錯萬能解決辦法

    這篇文章主要給大家介紹了關(guān)于java:程序包com.xxx.xxx不存在報錯萬能解決辦法,這個問題曾逼瘋初學(xué)者的我,不過弄清楚原理后就很簡單了,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • Java泛型機(jī)制的程序演示詳解

    Java泛型機(jī)制的程序演示詳解

    這篇文章主要為大家詳細(xì)介紹了Java泛型機(jī)制的程序演示,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Spring?Aop常見注解與執(zhí)行順序詳解

    Spring?Aop常見注解與執(zhí)行順序詳解

    這篇文章主要給大家介紹了關(guān)于Spring?Aop常見注解與執(zhí)行順序的相關(guān)資料,文中通過圖文以及實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-02-02

最新評論

沙雅县| 奇台县| 苍梧县| 社旗县| 汤原县| 太湖县| 汕头市| 黄浦区| 花垣县| 鄂州市| 兴海县| 安图县| 满城县| 嵊泗县| 新巴尔虎左旗| 金昌市| 澄江县| 湘潭县| 布拖县| 内乡县| 仪征市| 阜新| 北票市| 土默特右旗| 济阳县| 外汇| 威远县| 东源县| 汨罗市| 赣州市| 北安市| 合江县| 成安县| 工布江达县| 咸丰县| 长丰县| 嘉黎县| 邢台县| 安溪县| 德阳市| 库尔勒市|