java 函数式接口Consumer

目录

简介

accept方法

andThen方法

经典应用-iterable

扩展类介绍


简介

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

accept方法

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


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

具体使用例子


   
   
  1. Consumer<String> consumer= new Consumer<String>() {
  2. @Override
  3. public void accept(String t) {
  4. System.out.println(t+ "123");
  5. }
  6. };
  7. consumer.accept( "abc");

最后打印abc123

andThen方法

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


   
   
  1. /**
  2. * Returns a composed {@code Consumer} that performs, in sequence, this
  3. * operation followed by the {@code after} operation. If performing either
  4. * operation throws an exception, it is relayed to the caller of the
  5. * composed operation. If performing this operation throws an exception,
  6. * the {@code after} operation will not be performed.
  7. * <p>返回一个组合过的consumer实例,
  8. * <p>它的accept方法是先调用this这个consumer的accept方法,然后调用after的accept方法
  9. * <p>被抛出的异常会被转发给调用者,如果这个consumer的accept抛出异常,after不会被执行
  10. * @param after the operation to perform after this operation
  11. * <p>after是这个动作完成后,下一个做的动作
  12. * @return a composed {@code Consumer} that performs in sequence this
  13. * operation followed by the {@code after} operation
  14. * @throws NullPointerException if {@code after} is null
  15. */
  16. default Consumer<T> andThen(Consumer<? super T> after) {
  17. Objects.requireNonNull(after);
  18. return (T t) -> { accept(t); after.accept(t); };
  19. }

测试


   
   
  1. Consumer< String> consumer= new Consumer< String>() {
  2. @Override
  3. public void accept( String t) {
  4. System.out.println(t+ "123");
  5. }
  6. };
  7. consumer.accept( "abc");
  8. Consumer< String> consumer2= new Consumer< String>() {
  9. @Override
  10. public void accept( String t) {
  11. System.out.println(t+ "456");
  12. }
  13. };
  14. consumer2.accept( "abc");
  15. Consumer< String> consumer3=consumer.andThen(consumer2);
  16. consumer3.accept( "abc");

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


   
   
  1. abc123
  2. abc456
  3. abc123
  4. abc456

经典应用-iterable

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

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


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

扩展类介绍

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方法

猜你喜欢

转载自www.cnblogs.com/ZhangZiYangDeBoKe/p/11590651.html