欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-23 21:44:09
supplier接口package java.util.function;/** * represents a supplier of results. * *

there is n...

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

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