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);
  }
}

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

相关文章

  • 使用filter实现url级别内存缓存示例

    使用filter实现url级别内存缓存示例

    这篇文章主要介绍了使用filter实现url级别内存缓存示例,只需要一个静态类,在filter中调用,也可以全部写到filt里面。可以根据查询参数分别缓存,需要的朋友可以参考下
    2014-03-03
  • spring Roo安装使用简介

    spring Roo安装使用简介

    这篇文章主要介绍了spring Roo安装使用简介,具有一定借鉴价值,需要的朋友可以参考下
    2017-12-12
  • 关于Unsupported Media Type的解决方案

    关于Unsupported Media Type的解决方案

    在Web开发中,415错误表示服务器无法处理请求附带的媒体格式,本文介绍了导致HTTP 415错误的原因以及解决该问题的两种方法,首先,415错误通常是由于客户端请求的内容类型与服务器期望的不匹配引起的,例如,服务器可能期望JSON格式的数据
    2024-10-10
  • java8中的Collectors.groupingBy用法详解

    java8中的Collectors.groupingBy用法详解

    这篇文章主要介绍了java8中的Collectors.groupingBy用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • java获取反射机制的3种方法总结

    java获取反射机制的3种方法总结

    这篇文章主要给大家介绍了关于java获取反射机制的3种方法,文中通过示例代码介绍的非常详细,对大家学习或者使用java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-06-06
  • Java 代码本地设置Hadoop用户名密码的方法

    Java 代码本地设置Hadoop用户名密码的方法

    在Hadoop环境中,通常使用Kerberos进行身份验证,这篇文章主要介绍了Java 代码本地设置Hadoop用户名密码的方法,需要的朋友可以参考下
    2024-08-08
  • 详解Spring Cloud中Hystrix的请求合并

    详解Spring Cloud中Hystrix的请求合并

    这篇文章主要介绍了详解Spring Cloud中Hystrix的请求合并,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • java:程序包com.xxx.xxx不存在报错万能解决办法

    java:程序包com.xxx.xxx不存在报错万能解决办法

    这篇文章主要给大家介绍了关于java:程序包com.xxx.xxx不存在报错万能解决办法,这个问题曾逼疯初学者的我,不过弄清楚原理后就很简单了,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-12-12
  • Java泛型机制的程序演示详解

    Java泛型机制的程序演示详解

    这篇文章主要为大家详细介绍了Java泛型机制的程序演示,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • Spring Aop常见注解与执行顺序详解

    Spring Aop常见注解与执行顺序详解

    这篇文章主要给大家介绍了关于Spring Aop常见注解与执行顺序的相关资料,文中通过图文以及实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2022-02-02

最新评论