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方法产生一个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<? super T> after) {
    Objects.requireNonNull(after);
    return (T t) -> { accept(t); after.accept(t); };
  }
}

一个抽象方法accept(T t)定义了要执行的具体操作;注意看andThen方法,接收Consumer<? 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);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 聊聊Java 成员变量赋值和构造方法谁先执行的问题

    聊聊Java 成员变量赋值和构造方法谁先执行的问题

    这篇文章主要介绍了聊聊Java 成员变量赋值和构造方法谁先执行的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • javascript最新2020经典面试题

    javascript最新2020经典面试题

    这篇文章主要介绍了javascript最新2020经典面试题的相关内容,有需要的朋友们可以学习下。
    2020-02-02
  • Spring5新特性之Reactive响应式编程

    Spring5新特性之Reactive响应式编程

    这篇文章主要介绍了Spring5新特性之Reactive响应式编程,响应式编程是一种编程范式,通用和专注于数据流和变化的,并且是异步的,下文更多详细内容,需要的小伙伴可以参考一下,希望对你有所帮助
    2022-03-03
  • java设计模式--七大原则详解

    java设计模式--七大原则详解

    本篇文章主要对Java中的设计模式如,创建型模式、结构型模式和行为型模式以及7大原则进行了归纳整理,需要的朋友可以参考下,希望能给你带来帮助
    2021-07-07
  • SpringBoot 上传文件判空以及格式检验流程

    SpringBoot 上传文件判空以及格式检验流程

    这篇文章主要介绍了SpringBoot 上传文件判空以及格式检验流程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Java SpringBoot整合JSP和MyBatis

    Java SpringBoot整合JSP和MyBatis

    这篇文章主要介绍了SpringBoot如何整合JSP和MyBatis以及SpringBoot的基本设置,感兴趣的小伙伴可以参考阅读
    2023-03-03
  • java实现斐波那契数列的3种方法

    java实现斐波那契数列的3种方法

    这篇文章主要介绍了java实现斐波那契数列的3种方法,有需要的朋友可以参考一下
    2014-01-01
  • java实现简易连连看小游戏

    java实现简易连连看小游戏

    这篇文章主要为大家详细介绍了java实现简易连连看小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • java线程间通信的通俗解释及代码示例

    java线程间通信的通俗解释及代码示例

    这篇文章主要介绍了java线程间通信的通俗解释,介绍了线程通信中的几个相关概念,然后分享了线程通信的实现方式及代码示例,具有一定参考价值 ,需要的朋友可以了解下。
    2017-11-11
  • java中Iterator和ListIterator实例详解

    java中Iterator和ListIterator实例详解

    这篇文章主要介绍了java中Iterator和ListIterator实例详解,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12

最新评论