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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值