java 函数式接口Consumer

假如想象 提交于 2019-12-27 03:44:34

目录

简介

accept方法

andThen方法

经典应用-iterable

扩展类介绍


简介

Consumer是一个函数式接口,它有一个需要覆盖的方法accept,代表接受一个参数输入且没有任何返回值的操作。不同于其它的函数式接口,Consumer期望通过方法的实现来执行具体的操作

accept方法

可以看到它接受一个参数,什么都不返还

    /**     * Performs this operation on the given argument.     * <p>接受一个参数,覆盖的方法对它做出动作,什么都不返回     * @param t the input argument     */    void accept(T t);

具体使用例子

     Consumer<String> consumer=new Consumer<String>() {         @Override         public void accept(String t) {                   System.out.println(t+"123");            }        };       consumer.accept("abc");

最后打印abc123

andThen方法

这个方法返回一个组合过的consumer实例,
它的accept方法是先调用this这个consumer的accept方法,然后调用after的accept方法

    /**     * Returns a composed {@code Consumer} 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.     * <p>返回一个组合过的consumer实例,     * <p>它的accept方法是先调用this这个consumer的accept方法,然后调用after的accept方法     * <p>被抛出的异常会被转发给调用者,如果这个consumer的accept抛出异常,after不会被执行     * @param after the operation to perform after this operation      * <p>after是这个动作完成后,下一个做的动作     * @return a composed {@code Consumer} that performs in sequence this     * operation followed by the {@code after} operation     * @throws NullPointerException if {@code after} is null     */    default Consumer<T> andThen(Consumer<? super T> after) {        Objects.requireNonNull(after);        return (T t) -> { accept(t); after.accept(t); };    }

测试

     Consumer<String> consumer=new Consumer<String>() {           @Override            public void accept(String t) {                  System.out.println(t+"123");            }        };       consumer.accept("abc");          Consumer<String> consumer2=new Consumer<String>() {         @Override           public void accept(String t) {                 System.out.println(t+"456");           }       };      consumer2.accept("abc");               Consumer<String> consumer3=consumer.andThen(consumer2);        consumer3.accept("abc");

 看一看到consumer3的效果=consumer1后面执行consumer2

abc123abc456abc123abc456

经典应用-iterable

可以看到iterable接口的forEach方法使用了consumer类,传入一个consumer类型的参数action,

对iterable的所有元素作为参数执行下面的action.accept这个方法

    /**     * Performs the given action for each element of the {@code Iterable}     * until all elements have been processed or the action throws an     * exception.  Unless otherwise specified by the implementing class,     * actions are performed in the order of iteration (if an iteration order     * is specified).  Exceptions thrown by the action are relayed to the     * caller.     * <p>默认方法,对iterable的所有元素作为参数执行下面的action.accept这个方法,直到抛出异常     * <p>除非这个方法被覆盖的方法重写了,元素以iterator的顺序被执行     * <p>被抛出的异常会被转发给调用者     * @implSpec     * <p>这个方法等价于下面的这个代码     * <pre>{@code     *     for (T t : this)     *         action.accept(t);     * }</pre>     *     * @param action iterable里的所有元素,都作为参数,一个个给action对象,然后调用它的accept方法     * @throws NullPointerException if the specified action is null     * @since 1.8     */    default void forEach(Consumer<? super T> action) {        Objects.requireNonNull(action);        for (T t : this) {            action.accept(t);        }    }

扩展类介绍

Consumer的accept只接受一个参数,那如果要是想使用多个参数要怎么办?jdk8又提供了一个BiConsumer接口类,该类与Consumer的区别是可以接受2个参数。

jdk8还对Consumer和BiConsumer各提供了3个常用的相关接口类,见下表:

类名 描述
IntConsumer 接受单个int型参数的Consumer操作
DoubleConsumer 接受单个double型参数的Consumer操作
LongConsumer 接受单个long型参数的Consumer操作
ObjIntConsumer 接受2个int型参数的Consumer操作,不支持andThen方法
ObjDoubleConsumer 接受2个double型参数的Consumer操作,不支持andThen方法
ObjLongConsumer 接受2个long型参数的Consumer操作,不支持andThen方法

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!