Consumer

原文链接

1、Consumer API用法

  • java.util.function.Consumer:Consumer 接口有2个方法
    • accept:抽象方法。传入一个参数执行函数,但是不返回值
    • andThen:default方法。返回一个复合函数,按顺序执行该操作,后跟 after操作
  • JDK8在线源码英文文档:https://nowjava.com/readcode/jdk8
  • JDK8在线源码中文文档:https://www.matools.com/api/java8

1-1、accept

        Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println("消费:"+s);
            }
        };
        consumer.accept("cake");

        //lambda 表达式写法:
        consumer = (str)->{ System.out.println("消费:"+str); };
        consumer.accept("money");

1-2、andThen

        Consumer<String> first = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println("执行first: "+s);
            }
        };


        Consumer<String> second = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println("执行second: "+s);
            }
        };

        // 最终效果是一个接一个依次执行
        Consumer<String> compose = first.andThen(second);
        // 执行first: 123
        // 执行second: 123
        compose.accept("123");

2、源码翻译

  • 涉及到一个函数的副作用:side-effects
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预计将通过副作用进行操作。
 * side-effects的理解:
 *  函数的主要作用,y = 2x 函数的返回值是返回一个数的2倍,对每个自变量 x 计算出一个相对应的因变量 y。
 *  函数的副作用可能会比首要功能更重要。例如有 write() 这个函数,把一些字符写到一个文件里,传回true/false表示是否写成功,甚至不回传。
 *  这里明显可以看出,我们叫这个函数,主要还是希望它能把东西写到文件里的。
 *  可惜从数学的角度来讲,函数的首要功能永远都是返回值,所以这里“写文件”操作也被定义为“副作用”。
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #accept(Object)}.
 * 这是一个functional interface的功能方法是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 , 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
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值