前面的文章中说明了Java8中最基本的4种函数式接口,我们今天先介绍其它Cosumer函数式接口。
BiConsumer
通过前面的文章,我们了解到Consumer函数式接口中的accpet方法接受一个参数。而我们的BiConsumer函数式接口接受2个参数。该函数式接口为消费2个参数的行为提供了抽象。
/**
* Represents an operation that accepts two input arguments and returns no
* result. This is the two-arity specialization of {@link Consumer}.
* Unlike most other functional interfaces, {@code BiConsumer} 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, Object)}.
*
* @param <T> the type of the first argument to the operation
* @param <U> the type of the second argument to the operation
*
* @see Consumer
* @since 1.8
*/
@FunctionalInterface
public interface BiConsumer<T, U> {
/**
* Performs this operation on the given arguments.
*
* @param t the first input argument
* @param u the second input argument
*/
void accept(T t, U u);
/**
* Returns a composed {@code BiConsumer} 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 BiConsumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
Objects.requireNonNull(after);
return (l, r) -> {
accept(l, r);
after.accept(l, r);
};
}
}
该函数式接口同样提供了andThen方法,来组合消费行为。
DoubleConsumer
从类名上可以看出该函数式接口消费一个double参数。
/**
* Represents an operation that accepts a single {@code double}-valued argument and
* returns no result. This is the primitive type specialization of
* {@link Consumer} for {@code double}. Unlike most other functional interfaces,
* {@code DoubleConsumer} 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(double)}.
*
* @see Consumer
* @since 1.8
*/
@FunctionalInterface
public interface DoubleConsumer {
/**
* Performs this operation on the given argument.
*
* @param value the input argument
*/
void accept(double value);
/**
* Returns a composed {@code DoubleConsumer} 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 DoubleConsumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default DoubleConsumer andThen(DoubleConsumer after) {
Objects.requireNonNull(after);
return (double t) -> { accept(t); after.accept(t); };
}
}
IntConsumer
与DoubleConsumer类似,该函数式接口消费int 类型的参数。
/**
* Represents an operation that accepts a single {@code int}-valued argument and
* returns no result. This is the primitive type specialization of
* {@link Consumer} for {@code int}. Unlike most other functional interfaces,
* {@code IntConsumer} 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(int)}.
*
* @see Consumer
* @since 1.8
*/
@FunctionalInterface
public interface IntConsumer {
/**
* Performs this operation on the given argument.
*
* @param value the input argument
*/
void accept(int value);
/**
* Returns a composed {@code IntConsumer} 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 IntConsumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default IntConsumer andThen(IntConsumer after) {
Objects.requireNonNull(after);
return (int t) -> { accept(t); after.accept(t); };
}
}
LongConsumer
与DoubleConsumer和IntConsumer类似,该函数式接口消费long 类型的参数。
/**
* Represents an operation that accepts a single {@code long}-valued argument and
* returns no result. This is the primitive type specialization of
* {@link Consumer} for {@code long}. Unlike most other functional interfaces,
* {@code LongConsumer} 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(long)}.
*
* @see Consumer
* @since 1.8
*/
@FunctionalInterface
public interface LongConsumer {
/**
* Performs this operation on the given argument.
*
* @param value the input argument
*/
void accept(long value);
/**
* Returns a composed {@code LongConsumer} 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 LongConsumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default LongConsumer andThen(LongConsumer after) {
Objects.requireNonNull(after);
return (long t) -> { accept(t); after.accept(t); };
}
}
ObjDoubleConsumer/ObjIntConsumer/ObjLongConsumer
这三种函数式接口是特殊化的BiConsumer的接口,消费一个对象和一个基本类型。
@FunctionalInterface
public interface ObjDoubleConsumer<T> {
/**
* Performs this operation on the given arguments.
*
* @param t the first input argument
* @param value the second input argument
*/
void accept(T t, double value);
}
@FunctionalInterface
public interface ObjIntConsumer<T> {
/**
* Performs this operation on the given arguments.
*
* @param t the first input argument
* @param value the second input argument
*/
void accept(T t, int value);
}
@FunctionalInterface
public interface ObjLongConsumer<T> {
/**
* Performs this operation on the given arguments.
*
* @param t the first input argument
* @param value the second input argument
*/
void accept(T t, long value);
}
至此Java 8 中的8个Consumer函数接口已全部整理完,这些函数式接口为多种消费行为提供了抽象。
如何使用这些Consumer函数式接口,可以从Java 8 源码对Cosumer的使用上了解一二
例如:Iterator接口的默认方法forEachRemaining
/**
* Performs the given action for each remaining element until all elements
* have been processed or the action throws an exception. Actions are
* performed in the order of iteration, if that order is specified.
* Exceptions thrown by the action are relayed to the caller.
*
* @implSpec
* <p>The default implementation behaves as if:
* <pre>{@code
* while (hasNext())
* action.accept(next());
* }</pre>
*
* @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null
* @since 1.8
*/
default void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while (hasNext())
action.accept(next());
}
网友评论