October 29, 2022

Java Consumer Functional Interface Examples

In this post we’ll see examples of Java Consumer functional interface which is one of the built-in functional interface.

Consumer functional interface represents an operation that accepts a single argument and returns no result. Abstract method in this functional interface is accept(T t) and there is also one default method andThen().

Consumer functional interface example

In the example a List of integer is created and then elements of the List are displayed.

we’ll use lambda expression that implements the Consumer functional interface. Lambda expression you write implements the abstract method of the functional interface so in the case of Consumer functional interface, lambda expression is implementing the accept() method.

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class ConsumerExample {
  public static void main(String[] args) {
    Consumer<Integer> consumer = i -> System.out.println(i);
    List<Integer> numList = Arrays.asList(1, 2, 3, 4);
    for(Integer i : numList) {
      consumer.accept(i);
    }
  }
}
Output
1
2
3
4

Here this statement Consumer<Integer> consumer = i -> System.out.println(i + " "); is the implementation of Consumer as a lambda expression. Since the implementation is an instance of a functional interface so assigned to variable of type Consumer.

Lambda supports "target typing" which infers the object type from the context in which it is used. When consumer.accept() method is called Java can infer from the context where to look for the implementation of accept() method.

Consumer functional interface andThen() method example

There is also a default method andThen() in Consumer interface.

andThen(Consumer<? super T> after)- Takes another Consumer as argument and returns a composed Consumer that performs, in sequence, first the operation of the calling Consumer followed by the after operation.

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class ConsumerExample {
  public static void main(String[] args) {
    // First Consumer
    Consumer<String> consumer = s -> System.out.print("Original String- " + s);
    // Adding another consumer using andThen()
    Consumer<String> consumerAfter = consumer.andThen(s ->  System.out.print(" " + s.toUpperCase() + "\n"));
    List<String> nameList = Arrays.asList("Delhi", "Bangalore", "Hyderabad", "Lucknow");
    for(String str : nameList) {
      consumerAfter.accept(str);
    }
  }
}
Output
Original String- Delhi DELHI
Original String- Bangalore BANGALORE
Original String- Hyderabad HYDERABAD
Original String- Lucknow LUCKNOW

Here first Consumer prints the passed value and second consumer prints the same value in upper case.

andThen() method is called on the instance of the first Consumer and its argument is the second consumer. When the accept() method is called on the instance of the second consumer it calls in sequence initially the first consumer and then second consumer.

Consumer functional interface in JDK

These built-in functional interfaces are used extensively with in the JDK too. One example of Consumer functional interface used quite often is when forEach() method is used to iterate a collection. Method forEach() takes Consumer as argument.

forEach(Consumer<? super E> action)- Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

The examples shown above can be written more concisely using forEach().
public class ConsumerExample {
  public static void main(String[] args) {
    List<String> nameList = Arrays.asList("Delhi", "Bangalore", "Hyderabad", "Lucknow");
    nameList.forEach( s -> System.out.print(s + " "));
  }
}
Output
Delhi Bangalore Hyderabad Lucknow

That's all for the topic Java Consumer Functional Interface Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment