91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java8 Supplier接口和Consumer接口的用法

發布時間:2020-07-29 14:05:45 來源:億速云 閱讀:483 作者:小豬 欄目:編程語言

這篇文章主要講解了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方法產生一個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("創建實例");
  }
}

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<&#63; super T> after) {
    Objects.requireNonNull(after);
    return (T t) -> { accept(t); after.accept(t); };
  }
}

一個抽象方法accept(T t)定義了要執行的具體操作;注意看andThen方法,接收Consumer<&#63; super T>類型參數,返回一個lambda表達式,此表達式定義了新的執行過程,先執行當前Consumer實例的accept方法,再執行入參傳進來的Consumer實例的accept方法,這兩個accept方法接收都是相同的入參t。

實例:

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("之后執行:"+s);
    };
    consumer.andThen(consumerAfter).accept(5);
  }
}

看完上述內容,是不是對Java8 Supplier接口和Consumer接口的用法有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

南川市| 叙永县| 涿州市| 虞城县| 博兴县| 东宁县| 辛集市| 独山县| 西平县| 沙田区| 祁门县| 县级市| 南靖县| 洱源县| 肇东市| 武隆县| 常熟市| 临海市| 吴忠市| 玉田县| 封开县| 汉阴县| 横峰县| 昌吉市| 岫岩| 开化县| 荥阳市| 蒙城县| 葫芦岛市| 瓦房店市| 镇坪县| 徐闻县| 郎溪县| 古浪县| 分宜县| 西青区| 海口市| 诸暨市| 赤水市| 容城县| 海林市|