欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年04月28日 14:28:37   作者:Terry  
這篇文章主要介紹了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);
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺析Java和Scala中的Future

    淺析Java和Scala中的Future

    這篇文章主要介紹了Java和Scala中的Future的相關(guān)資料,需要的朋友可以參考下
    2017-10-10
  • 在windows環(huán)境下安裝jdk8、jdk9、jdk11、jdk12并自由切換

    在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登錄界面示例分享

    這篇文章主要介紹了使用java swing實現(xiàn)qq登錄界面示例,需要的朋友可以參考下
    2014-04-04
  • Springboot如何使用Map將錯誤提示輸出到頁面

    Springboot如何使用Map將錯誤提示輸出到頁面

    這篇文章主要介紹了Springboot如何使用Map將錯誤提示輸出到頁面,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • springboot清除字符串前后空格與防xss攻擊方法

    springboot清除字符串前后空格與防xss攻擊方法

    這篇文章主要介紹了springboot清除字符串前后空格與防xss攻擊方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 如何使用SpringBootCondition更自由地定義條件化配置

    如何使用SpringBootCondition更自由地定義條件化配置

    這篇文章主要介紹了如何使用SpringBootCondition更自由地定義條件化配置,幫助大家更好的理解和學習使用springboot框架,感興趣的朋友可以了解下
    2021-04-04
  • 關(guān)于java String中intern的深入講解

    關(guān)于java String中intern的深入講解

    這篇文章主要給大家介紹了關(guān)于java String中intern的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用java具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-04-04
  • Druid核心源碼解析DruidDataSource

    Druid核心源碼解析DruidDataSource

    這篇文章主要為大家介紹了Druid核心源碼解析DruidDataSource,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • Spring Cloud Ubuntu環(huán)境部署的步驟與注意事項

    Spring Cloud Ubuntu環(huán)境部署的步驟與注意事項

    這篇文章主要給大家介紹了關(guān)于Spring Cloud Ubuntu環(huán)境部署的步驟與注意事項,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Cloud具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-12-12
  • Java 集合框架之List 的使用(附小游戲練習)

    Java 集合框架之List 的使用(附小游戲練習)

    這篇文章主要介紹Java 集合框架中List 的使用,下面文章將圍繞Java 集合框架中List 的使用展開話題,并附上一些小游戲練習,需要的朋友可以參考一下
    2021-10-10

最新評論