Java 8 Consumer and Supplier Example
Java 8 introduced several functional interfaces in the java.util.function
package to support functional programming paradigms. Among these interfaces, Consumer
and Supplier
play vital roles by representing functions with specific input and output requirements.
In this article, we will explore the differences between Consumer
and Supplier
and provide examples to illustrate their usage.
1. Consumer
The Consumer<T>
interface is designed to perform operations on objects without returning any result. It accepts a single input of type T
and executes the desired action. The key method of the Consumer
interface is accept(T t)
, which takes an argument of type T
.
Let’s consider an example where we have a list of strings and we want to print each string on a separate line:
package org.websparrow;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class ConsumerExample {
public static void main(String[] args) {
Consumer<String> printString = (str) -> System.out.println(str);
List<String> stringList = Arrays.asList("India", "USA", "Thailand");
stringList.forEach(printString);
}
}
In the above example, we define a Consumer
called printString
, which takes a string as input and prints it to the console. We then create a list of strings and use the forEach
method to apply the printString
consumer to each element of the list.
Output:
India
USA
Thailand
Similar Post: Java 8 Predicate vs Function: A Comparison with Examples
2. Supplier
The Supplier<T>
interface, on the other hand, represents a supplier of results of type T
. It does not take any input and is responsible for generating or providing values when called. The primary method in the Supplier
interface is get()
, which returns a result of type T
.
Let’s consider an example where we generate a random number using the Supplier
interface:
package org.websparrow;
import java.security.SecureRandom;
import java.util.function.Supplier;
public class SupplierExample {
public static void main(String[] args) {
Supplier<Integer> randomNumber = () -> new SecureRandom().nextInt();
Integer value = randomNumber.get();
System.out.println("Random Number: " + value);
}
}
In the above example, we define a Supplier
called randomNumber
, which generates a secure random number using the SecureRandom
class. We then call the get()
method on the randomNumber
supplier to obtain the generated random number and print it to the console.
Output:
Random Number: 1461150494
Summary
The Consumer
interface accepts an input of type T
and performs some action on it, whereas the Supplier
interface provides a result of type T
without taking any input. Consumer
is useful when you need to perform operations on objects or process a collection of elements, while Supplier
is handy for generating or providing values on demand.
In Java 8, these functional interfaces, along with other interfaces in the java.util.function
package, enable developers to write more concise and expressive code by leveraging functional programming concepts. Understanding the distinctions between Consumer
and Supplier
allows you to utilize them effectively in various scenarios, enhancing the flexibility and readability of your Java code.