常见函数式接口

HeJin小于 1 分钟函数式编程函数式接口

Consumer消费接口

对传入的参数进行消费

@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

Function计算转换接口

对传入的参数进行计算或者转换,把结果返回。

@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);

Predicate判断接口

对传入的参数条件判断,返回判断结果。

@FunctionalInterface
public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);

Supplier生产型接口

可以在方法中创建对象,把创建好的对象返回。

@FunctionalInterface
public interface Supplier<T> {

    T get();
}