美文网首页
函数式接口(三)

函数式接口(三)

作者: 西安法律咨询服务平台与程序员 | 来源:发表于2019-04-09 17:40 被阅读0次

前面的文章中说明了Java8中最基本的4种函数式接口,我们今天先介绍其它Supplier和Predicate以及Function函数式接口。

BooleanSupplier/DoubleSupplier/IntSupplier/LongSupplier

BooleanSupplier/DoubleSupplier/IntSupplier/LongSupplier这4个函数式接口,为获得基本类型的函数式接口。

BiPredicate

BiPredicate函数式接口的test方法接收2个参数。

/**
 * Represents a predicate (boolean-valued function) of two arguments.  This is
 * the two-arity specialization of {@link Predicate}.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #test(Object, Object)}.
 *
 * @param <T> the type of the first argument to the predicate
 * @param <U> the type of the second argument the predicate
 *
 * @see Predicate
 * @since 1.8
 */
@FunctionalInterface
public interface BiPredicate<T, U> {

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

    /**
     * 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.
     *
     * <p>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 BiPredicate<T, U> and(BiPredicate<? super T, ? super U> other) {
        Objects.requireNonNull(other);
        return (T t, U u) -> test(t, u) && other.test(t, u);
    }

    /**
     * Returns a predicate that represents the logical negation of this
     * predicate.
     *
     * @return a predicate that represents the logical negation of this
     * predicate
     */
    default BiPredicate<T, U> negate() {
        return (T t, U u) -> !test(t, u);
    }

    /**
     * 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.
     *
     * <p>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 BiPredicate<T, U> or(BiPredicate<? super T, ? super U> other) {
        Objects.requireNonNull(other);
        return (T t, U u) -> test(t, u) || other.test(t, u);
    }
}

DoublePredicate/IntPredicate/LongPredicate

这几个函数式接口均是特殊化的Predicate接口

BiFunction

BiFunction与Function函数式接口的主要区别是:apply方法接收2个入参。

/**
 * Represents a function that accepts two arguments and produces a result.
 * This is the two-arity specialization of {@link Function}.
 *
 * <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 first argument to the function
 * @param <U> the type of the second argument to the function
 * @param <R> the type of the result of the function
 *
 * @see Function
 * @since 1.8
 */
@FunctionalInterface
public interface BiFunction<T, U, R> {

    /**
     * Applies this function to the given arguments.
     *
     * @param t the first function argument
     * @param u the second function argument
     * @return the function result
     */
    R apply(T t, U u);

    /**
     * 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
     */
    default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t, U u) -> after.apply(apply(t, u));
    }
}

DoubleFunction/IntFunction/LongFunction

DoubleFunction/IntFunction/LongFunction这三个函数式接口为特殊的Function接口。

LongToDoubleFunction/LongToIntFunction

LongToDoubleFunction/LongToIntFunction这两个函数式接口的apply方法为入参为long类型,出参为其它基本类型的Function。

ToDoubleFunction/ToIntFunction/ToLongFunction

ToDoubleFunction/ToIntFunction/ToLongFunction这三个函数式接口的apply方法为入参为其它类型,出参为基本类型的Function.

ToDoubleBiFunction/ToIntBiFunction/ToLongBiFunction

ToDoubleBiFunction/ToIntBiFunction/ToLongBiFunction这三个函数式接口的apply方法有2个入参,出参为基本类型的Function。

相关文章

  • java基础-day23-函数式接口和Stream流

    函数式接口和Stream 1. 函数式接口 1.1 函数式接口概述 1.2 常用函数式接口 1.3 比较器函数式接...

  • 2020-07-04【函数式接口】

    函数式接口概述 函数式接口作为方法的参数 函数式接口作为方法的返回值 常见的函数式接口 Supplier接口 Co...

  • 12.函数式接口

    主要内容 自定义函数式接口 函数式编程 常用函数式接口 第一章 函数式接口 1.1 概念 函数式接口在Java中是...

  • 函数式接口

    函数式接口 一、函数式接口的定义   函数式接口:函数式接口中有且仅有一个抽象方法,这个抽象方法的意义在于表达某种...

  • 测验:函数式接口

    下面哪些接口是函数式接口? 答案:只有Adder是函数式接口。SmartAdder不是函数式接口,因为它定义了两个...

  • Java8系列:神奇的函数式接口

    01 函数式接口是什么? 有且只有一个抽象方法的接口被称为函数式接口,函数式接口适用于函数式编程的场景,Lambd...

  • 函数式接口和Lambda表达式深入理解

    0x00 函数式接口 前面讲了一下函数式接口,不过可能只是讲了个大概,大致讲了一下什么是函数式接口 函数式接口就是...

  • java8函数式接口

    一、函数式接口 1、函数式接口在java中是指:有且仅有一个抽象方法的接口,函数式接口,即适用于函数式编程场景的接...

  • Java 8 知多少

    一、函数式接口 函数式接口的定义: 函数式接口(Functional Interface)就是一个有且仅有一个抽象...

  • Java lambda表达式

    1. Java函数式接口 Java实现函数式编程的方式是函数式接口(functional interface),函...

网友评论

      本文标题:函数式接口(三)

      本文链接:https://www.haomeiwen.com/subject/harbiqtx.html