目录
1.2.2 Consumer andThen(Consumer after)方法
前言
java sdk package: package java.util.function;
一 Consumer<T>
1.1 源码
注释翻译过来大致为:这个Consumer<T>接口表示(或者定义)了对输入参数的一系列处理规则,这个操作需要有一个输入参数,就是一个没有返回值的匿名函数。
/**
* 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
*/
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
// 将入参T,按照你定义的规则进行处理
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); };
}
}
1.2 accept和andThen方法
1.2.1 accept(T t)方法
接收一个入参,按照定义的规则处理入参;
举例:
public static void main(String[] args) {
String test1= "我";
String test2= "你";
String test3= "大家";
StringBuilder builderInput = new StringBuilder("我要买");
Consumer<String> consumer = (string)-> System.out.println(string + "爱中国");
consumer.accept(test1);
consumer.accept(test2);
consumer.accept(test3);
Consumer<StringBuilder> consumer2 = (builder)-> builder.append("玛莎拉蒂");
System.out.println("accept前: "+builderInput.toString());
consumer2.accept(builderInput);
System.out.println("accept后: "+builderInput.toString());
}
1.2.2 Consumer<T> andThen(Consumer<? super T> after)方法
多个Consumer的组合,定义的多个处理流程,链式化处理(流水线作业)。输入是一个规则,返回一个新的规则。
先判断入参的Consumer<? super T>after是不是null,为null会抛出空指针异常。然后,先执行Consumer自身定义的规则,最后执行输入的Consumer的规则。
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
public static void main(String[] args) {
Consumer<StringBuilder> consumer1 = (builder)-> builder.append("->步骤1");
Consumer<StringBuilder> consumer2 = (builder)-> builder.append("->步骤2");
Consumer<StringBuilder> consumer3 = (builder)-> builder.append("->步骤3");
Consumer<StringBuilder> newConsumer = consumer1.andThen(consumer2).andThen(consumer3);
StringBuilder builder = new StringBuilder("原始原件");
newConsumer.accept(builder);
System.out.println(builder.toString());
}
1.3.3 其他Consumer
IntConsumer、DoubleConsumer以及LongConsumer等。
二 BiConsumer<T, U>
2.1 源码
和Consumer<T>的唯一区别,就是接收的入参是两个
public interface BiConsumer<T, U> {
void accept(T t, U u);
default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
Objects.requireNonNull(after);
return (l, r) -> {
accept(l, r);
after.accept(l, r);
};
}
}