andThen的用法。
@Test
public void lambdaFc(){
Consumer <Student>con1=(Student s)->{
out.println(s.getName());
s.setName("李四");
out.println(s.getName());
};
Consumer <Student>con2=(Student s) ->{
out.println(s.getName()); };
Consumer con3=con1.andThen(con2);
con3.accept(new Student());
}
输出结果
Consumer接口源代码
@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); };
}
andThen方法返回了一个新的Consumer对象。
所以 Consumer con3=con1.andThen(con2);
这行代码就是用于生成一个对象con3,持有了con1,con2的引用。并且依次调用con1,con2的accept方法;注意的是con1,con2,持有的是同一个入参。
那么这个接口的意义在哪里呢。
stream有一个中间操作 & 终止操作的概念,中间操作返回本身类型,终止操作返回非调用者本身类型。在这里也类似。andThen返回的是Consumer类型,那么就可以嵌套一直传递下去。通过这种方式就可以组合方法,把一个个accept方法组合起来,按照我们想要的顺序调用。类似的还有Consumer
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
/**
* Returns a composed function that first applies the {@code before}
* function to its input, and then applies this function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of input to the {@code before} function, and to the
* composed function
* @param before the function to apply before this function is applied
* @return a composed function that first applies the {@code before}
* function and then applies this function
* @throws NullPointerException if before is null
*
* @see #andThen(Function)
*/
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
/**
* Returns a composed function that first applies this function to
* its input, and then applies the {@code after} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of output of the {@code after} function, and of the
* composed function
* @param after the function to apply after this function is applied
* @return a composed function that first applies this function and then
* applies the {@code after} function
* @throws NullPointerException if after is null
*
* @see #compose(Function)
*/
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
/**
* Returns a function that always returns its input argument.
*
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
*/
static <T> Function<T, T> identity() {
return t -> t;
}
}
这里面的andThen方法和Consumer一样返回了本身接口类型匿名对象。
从这里我们可以看出andThen。构成一种链式调用。
@Test
public void FunctionT(){
Function<Integer,String> fIntToDouble=(i)->i.toString();
Function<String,Long> DoubleToString=(i)->Long.valueOf(i);
Function <Long,Integer> StringToLong=(i)->i.intValue();
Integer l= fIntToDouble.andThen(DoubleToString).andThen(StringToLong).apply(11);
}
上面的样例代码可以通过不同的组合方式去转换。