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

函数式接口(二)

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

前面的文章中说明了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());
    }

相关文章

  • 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中是指:有且仅有一个抽象方法的接口,函数式接口,即适用于函数式编程场景的接...

  • 函数式接口(二)

    前面的文章中说明了Java8中最基本的4种函数式接口,我们今天先介绍其它Cosumer函数式接口。 BiConsu...

  • Java 8 知多少

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

网友评论

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

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