Java Consumer Interface

someone can help me understand below code from Java 8 Functional Interface
As per my understanding accept() takes as input and process it but does not return any value then in case of andThen() how it works

accept() method takes as input the type T and returns no value.

default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
}

3
Leave a Reply

avatar
3 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

A functional interface must have only one abstract method. However, it can have as many static and default methods as you’d like. The methods of Consumer are: accept(T) This is the single abstract method of Consumer. It accepts a single generic argument of type T and returns nothing (i.e. void). This is the method that’s implemented by a lambda expression or method reference. andThen(Consumer) This is a default method. In other words, it has an implementation and is thus non-abstract. The method accepts a Consumer and returns another Consumer. Since it’s a default method, the single abstract method of Consumer… Read more »

Jason
Guest

In order to understand what is getting returned from that API, you can try visualizing the implementation as : default Consumer<T> andThen(Consumer<? super T> after) { Objects.requireNonNull(after); return new Consumer<T>() { // return the complete Consumer implementation @Override public void accept(T t) { Consumer.this.accept(t); // accept current consumer after.accept(t); // and then accept the 'after' one. } }; } and now relate that (T t) -> { accept(t); after.accept(t); } is a Consumer returned which ensures that the current one is accepted first and then the one mentioned as after.

Jason
Guest

I think I understand your concern. You wonder why could you call andThen() after accept() if accept() returns void.

The thing is, that you first define a Consumer object, and on that instance you call andThen() method. For example:

Consumer<String> consumer = s -> System.out.println(s);
consumer.andThen(s -> System.out.println(s.toUpperCase()));