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

所属分类: 软件编程 / java 阅读数: 1032
收藏 0 赞 0 分享

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解决农夫过河问题的相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

详解java中的四种代码块

这篇文章主要介绍了详解java中的四种代码块,具有一定借鉴价值,需要的朋友可以参考下。
收藏 0 赞 0 分享

Java 9中如何对IntegerCache进行修改详解

这篇文章主要给大家介绍了关于Java 9中如何对IntegerCache进行修改的相关资料,文中通过示例代码介绍的非常详细,对大家学习或使用java9具有一定的参考学习价值,需要的朋友们下面随着小编来一起看看吧。
收藏 0 赞 0 分享

关于Java中你所不知道的Integer详解

这篇文章主要给大家介绍了关于Java中你所不知道的一些关于Integer的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

JavaWeb基于Session实现的用户登陆注销方法示例

为了安全起见,session常常用来保存用户的登录信息。那么服务器是怎么来实现的呢?下面这篇文章就来给大家介绍了关于JavaWeb基于Session实现的用户登陆注销的相关资料,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

详谈Java中net.sf.json包关于JSON与对象互转的坑

下面小编就为大家分享一篇Java中net.sf.json包关于JSON与对象互转的坑,具有很好的参考价值,希望对大家有所帮助
收藏 0 赞 0 分享

Springboot整合pagehelper分页功能

这篇文章主要为大家详细介绍了Springboot整合pagehelper分页功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解eclipse中Maven工程使用Tomcat7以上插件的方法

本篇文章主要介绍了详解eclipse中Maven工程使用Tomcat7以上插件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

堆排序实例(Java数组实现)

下面小编就为大家分享一篇使用Java数组实现堆排序的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java基于递归和循环两种方式实现未知维度集合的笛卡尔积算法示例

这篇文章主要介绍了Java基于递归和循环两种方式实现未知维度集合的笛卡尔积算法,结合实例形式分析了Java使用递归与循环两种方式实现未知维度集合的笛卡尔积相关概念、原理与操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多