Consumer Interface function java

table of Contents

Brief introduction

accept method

andThen method

Classic applications -iterable

Extended class presentation


Brief introduction

Consumer is a function interface, a method which has a need to cover accept, accept a parameter representative of the input and the operation returns no value . Unlike other interface functions, Consumer is desirable to perform a specific operation by the implementation of the method .

accept method

You can see it takes one argument, nothing is returned


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

Specific examples of use


   
   
  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");

Finally, print abc123

andThen method

This method returns a combination of a peroxide of consumer instance,
it is to accept the first call accept this method consumer then calls the method after the 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. }

test


   
   
  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");

 = Look back to consumer3 effect consumer1 performed consumer2


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

Classic applications -iterable

The method can be seen forEach iterable interface class consumer use, consumer pass a parameter of type Action,

All elements of iterable performed following this method as a parameter 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. }

Extended class presentation

Consumer's accept only accepts one parameter, that If we want to use multiple parameters to be how to do? jdk8 also provides a BiConsumer interface class, the class distinction is acceptable Consumer two parameters.

Consumer and BiConsumer jdk8 also provides three respective common interfaces classes, as follows:

The class name description
IntConsumer Accepts a single parameter of type int operation Consumer
DoubleConsumer Accepts a single parameter of type double operation Consumer
LongConsumer Accepts a single parameter of type long operation Consumer
ObjIntConsumer Accepts two parameters of type int Consumer operation, the method does not support andThen
ObjDoubleConsumer Accepts two arguments of type double Consumer operation, the method does not support andThen
ObjLongConsumer Accepts two parameters Consumer long type operation, the method does not support andThen

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/ZhangZiYangDeBoKe/p/11590651.html