java/函数式接口-supplier

1 Supplier理解

1.1 JavaDoc阅读

 

 

 

/**
 * Represents a supplier(供应者) of results.
 *
 * <p>There is no requirement that a new or distinct result be returned each
 * time the supplier is invoked.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #get()}.
 *
 * @param <T> the type of results supplied by this supplier
 *
 * @since 1.8
 */

1.2 Supplier实例理解

 

第一种采用的是lambda的方式,第二种则是方法引用(类名::构造方法)-》不输入参数,返回一个值

 

 

2 扩展BinaryOperator

 

 

 

同时可以看到BinaryOperator继承了BiFunction,同时类型完全一致

/**
 * Represents an operation upon two operands(两个操作数) of the same type, producing a result
 * of the same type as the operands.  This is a specialization(特例化) of
 * {@link BiFunction} for the case where the operands and the result are all of
 * the same type.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #apply(Object, Object)}.
 *
 * @param <T> the type of the operands and result of the operator
 *
 * @see BiFunction
 * @see UnaryOperator
 * @since 1.8
 */

/**
     * Returns a {@link BinaryOperator} which returns the lesser of two elements
     * according to the specified {@code Comparator}.
     *
     * @param <T> the type of the input arguments of the comparator
     * @param comparator a {@code Comparator} for comparing the two values
     * @return a {@code BinaryOperator} which returns the lesser of its operands,
     *         according to the supplied {@code Comparator}
     * @throws NullPointerException if the argument is null
     */

3 扩展Optional

 

 

 

/**
 * A container object(容器对象) which may or may not contain a non-null value.
 * If a value is present, {@code isPresent()} will return {@code true} and
 * {@code get()} will return the value.
 *
 * <p>Additional methods that depend on the presence or absence of a contained
 * value are provided, such as {@link #orElse(java.lang.Object) orElse()}
 * (return a default value if value not present) and
 * {@link #ifPresent(java.util.function.Consumer) ifPresent()} (execute a block
 * of code(执行代码块) if the value is present).
 *
 * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
 * class; use of identity-sensitive operations (including reference equality
 * ({@code ==}), identity hash code, or synchronization) on instances of
 * {@code Optional} may have unpredictable results and should be avoided.
 *
 * @since 1.8
 */

 

 

 

 

 

 

 

 

 

 

根据返回类型,确定是否需要返回空数组

 

posted @ 2021-02-24 01:06  谁给起个名  阅读(362)  评论(0)    收藏  举报