Java8 提供的函数接口

参考资料

函数接口

Java 提供的函数接口位于 java.util.function 包下。

接下来介绍主要的函数接口:

Function 接口代表一个 接收1个参数并返回1个结果的函数。

Function 接口源码:

@FunctionalInterface
public interface Function {

    /**
     * 将参数 t 应用到函数,并返回结果
     *
     * @param t 函数参数
     * @return 函数结果
     */
    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  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  Function compose(Function 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  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  Function andThen(Function after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    /**
     * 静态方法,生成一个结果等于输入参数的函数
     */
    static  Function identity() {
        return t -> t;
    }
}

Predicate 接口代表对一个参数的断言。
Predicate 接口代码:

@FunctionalInterface
public interface Predicate {

    /**
     * 对入参 t 进行断言(断言,相当于判断)
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);

    /**
     * Returns a composed predicate that represents a short-circuiting logical
     * AND of this predicate and another.  When evaluating the composed
     * predicate, if this predicate is {@code false}, then the {@code other}
     * predicate is not evaluated.

     *
     * Any exceptions thrown during evaluation of either predicate are relayed
     * to the caller; if evaluation of this predicate throws an exception, the
     * {@code other} predicate will not be evaluated.

     *
     * @param other a predicate that will be logically-ANDed with this
     *              predicate
     * @return a composed predicate that represents the short-circuiting logical
     * AND of this predicate and the {@code other} predicate
     * @throws NullPointerException if other is null
     */
    default Predicate and(Predicate other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }

    /**
     * Returns a predicate that represents the logical negation of this
     * predicate.

     *
     * @return a predicate that represents the logical negation of this
     * predicate
     */
    default Predicate negate() {
        return (t) -> !test(t);
    }

    /**
     * Returns a composed predicate that represents a short-circuiting logical
     * OR of this predicate and another.  When evaluating the composed
     * predicate, if this predicate is {@code true}, then the {@code other}
     * predicate is not evaluated.

     *
     * Any exceptions thrown during evaluation of either predicate are relayed
     * to the caller; if evaluation of this predicate throws an exception, the
     * {@code other} predicate will not be evaluated.

     *
     * @param other a predicate that will be logically-ORed with this
     *              predicate
     * @return a composed predicate that represents the short-circuiting logical
     * OR of this predicate and the {@code other} predicate
     * @throws NullPointerException if other is null
     */
    default Predicate or(Predicate other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

    /**
     * Returns a predicate that tests if two arguments are equal according
     * to {@link Objects#equals(Object, Object)}.

     *
     * @param  the type of arguments to the predicate
     * @param targetRef the object reference with which to compare for equality,
     *               which may be {@code null}
     * @return a predicate that tests if two arguments are equal according
     * to {@link Objects#equals(Object, Object)}
     */
    static  Predicate isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }
}

Supplier 接口代表结果的供应商。
Supplier 接口代码:

@FunctionalInterface
public interface Supplier {

    /**
     * 获取一个结果
     *
     * @return a result
     */
    T get();
}

UrnaryOperator 接口代表一元操作符,即返回结果类型和输入参数类型一样。
UnaryOperator 接口代码:


@FunctionalInterface
public interface UnaryOperator extends Function {

    /**
     * 返回结果等于入参的一元操作符
     *
     * @param  the type of the input and output of the operator
     * @return a unary operator that always returns its input argument
     */
    static  UnaryOperator identity() {
        return t -> t;
    }
}

Original: https://www.cnblogs.com/lihw-study/p/16725276.html
Author: 不安分的黑娃
Title: Java8 提供的函数接口

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/593469/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球