Java Consumer源码总结 Consumer源码注释翻译和解析中英文对照版

版本
JDK8(JDK1.8)

Consumer函数式接口源码重点
1.Consumer函数式接口需要实现的方法是 void accept(T t),表示一个接受单个输入参数但不返回结果的操作。
2.Consumer andThen(Consumer<? super T> after) 用于拼接多个Consumer的accept(T t)方法,按顺序执行操作,如果前面的操作抛出异常,则后面的操作不会执行

3.Consumer接口方法

方法名作用
void accept(T t)对给定参数执行此操作
Consumer andThen(Consumer<? super T> after)返回一个组合的Consumer,它依次执行此操作和after操作

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.
 * 表示接受单个输入参数但不返回结果的操作。
 * 与大多数其他功能接口不同,Consumer预期通过副作用进行操作。
 * 
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #accept(Object)}.
 * 其函数方法是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.
     * 返回一个组合的Consumer,它依次执行此操作和after操作。
     * 如果执行任何一个操作都会引发异常,则会将异常转发给组合操作的调用方。
     * 如果执行此操作引发异常,则不会执行after操作。
     * 
     * 
     *
     * @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
     * 一种组合的Consumer,按顺序执行此操作,然后执行after操作
     * @throws NullPointerException if {@code after} is null 如果after为null
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lolxxs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值