Consumer Functional Interface in Java 8 with Examples

In Java 8, The Consumer Functional Interface in Java 8 is a functional interface that represents an operation that accepts a single input argument and returns no result. It belongs to the java.util.function package and can be used to pass a behavior as a parameter to methods, making the code more modular and reusable.

Table of Content :

The Consumer interface has a single abstract method called accept(), which takes an argument of a generic type and performs an operation on it. The syntax of the Consumer functional interface is as follows:

@FunctionalInterface public interface Consumer<T> { void accept(T t); }

Here, T is the type of the input argument that the Consumer accepts.

Below is an example of using the Consumer interface to print a list of integers:

package com.javacodestuffs.core.java.functional.iface.consumer; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; public class ConsumerExample { public static void main(String[] args) { List<String> strList = Arrays.asList("Bala", "Anna", "Charlie", "Joseph", "Sara", "Shivani"); // lambda expression to implement the accept() method Consumer<String> strList = (String str) -> System.out.println(str); // forEach() method to iterate over the list and apply the printNumber consumer // to each element strList.forEach(printNumber); } }

Output

Bala Anna Charlie Joseph Sara Shivani

Consumer Functional Interface in Java 8 - Example2

package com.javacodestuffs.core.java.functional.iface.consumer; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; public class ConsumerNumberExample { public static void main(String[] args) { List<Integer> strList = Arrays.asList(11,22,33,44,66,77,99); // lambda expression to implement the accept() method Consumer<Intege> printNumber = (Integer num) -> System.out.println(num); // forEach() method to iterate over the list and apply the printNumber consumer // to each element strList.forEach(printNumber); } }

Output

11 22 33 44 66 77 99

Advantages to use Consumer Functional Interface

The Consumer Functional Interface in Java 8 provides several advantages over traditional approaches to handling behavior as parameters to methods:

  • Reusability: By defining behavior in a separate class or lambda expression, that behavior can be reused across multiple methods, making code more modular and easier to maintain.
  • Flexibility: The Consumer interface provides a flexible way to define behavior that can be easily adapted to different use cases. By changing the implementation of the accept() method, the behavior of the Consumer can be customized to fit different needs.
  • Readability: Using a lambda expression or method reference to implement a Consumer can make code more readable and concise, especially when compared to using anonymous inner classes.
  • Separation of Concerns: The Consumer interface allows behavior to be separated from the business logic of a method, making it easier to focus on the core functionality of the method and reducing code complexity.
  • Parallel Execution: Java 8 introduced a number of stream APIs that use Consumer as a parameter to perform parallel processing. By providing a standard interface for passing behavior, Java 8 made it easier to leverage the power of multicore processors and improve the performance of Java applications.

Conclusion

Overall, the Consumer Functional Interface in Java 8 provides a powerful and flexible way to define behavior as a parameter to methods, making Java code more modular, reusable, and performant.