The Consumer<T>
interface in Java is a functional interface from the java.util.function
package. It represents an operation that accepts a single input argument and does not return any result. It is commonly used for operations where a value is passed in and some side effect occurs (e.g., printing, modifying state, or logging).
Steps to Use a Consumer:
- Functional Interface: Since
Consumer
is a functional interface, you can use it with lambda expressions, method references, or anonymous classes. - Method: It has a single abstract method:
void accept(T t)
: Performs the operation on the given input.
Example Usage
Here are several ways we can use the Consumer<T>
interface:
1. Using Lambda Expressions
package org.kodejava.util.function;
import java.util.function.Consumer;
public class ConsumerExample {
public static void main(String[] args) {
Consumer<String> printConsumer = s -> System.out.println(s);
// Output: Hello, Consumer!
printConsumer.accept("Hello, Consumer!");
}
}
2. Using Method References
package org.kodejava.util.function;
import java.util.function.Consumer;
public class ConsumerExample2 {
public static void main(String[] args) {
// Referencing the println method
Consumer<String> printConsumer = System.out::println;
// Output: Hello, Method Reference!
printConsumer.accept("Hello, Method Reference!");
}
}
3. Using Anonymous Classes
package org.kodejava.util.function;
import java.util.function.Consumer;
public class ConsumerExample3 {
public static void main(String[] args) {
Consumer<String> printConsumer = new Consumer<String>() {
@Override
public void accept(String t) {
System.out.println(t);
}
};
// Output: Hello, Anonymous Class!
printConsumer.accept("Hello, Anonymous Class!");
}
}
4. Using with andThen
for Chaining
The Consumer
interface provides a default method andThen
that allows chaining multiple Consumers in sequence.
package org.kodejava.util.function;
import java.util.function.Consumer;
public class ConsumerExample4 {
public static void main(String[] args) {
Consumer<String> printConsumer = s -> System.out.println("Printing: " + s);
Consumer<String> lengthConsumer = s -> System.out.println("Length: " + s.length());
// Chaining Consumers
Consumer<String> chainedConsumer = printConsumer.andThen(lengthConsumer);
chainedConsumer.accept("Hello, Chaining!");
// Output:
// Printing: Hello, Chaining!
// Length: 16
}
}
5. Using with Collections
Consumer is commonly used with the forEach
method of Java collections.
package org.kodejava.util.function;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class ConsumerExample5 {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Carol");
// Using forEach with Consumer
Consumer<String> printName = name -> System.out.println("Hello, " + name + "!");
names.forEach(printName);
// Output:
// Hello, Alice!
// Hello, Bob!
// Hello, Carol!
}
}
6. A Real-World Example
We might use a Consumer<T>
in logging operations, updating GUI elements, or applying modifications to a list of objects.
package org.kodejava.util.function;
import java.util.function.Consumer;
public class LoggingExample {
public static void main(String[] args) {
Consumer<String> logger = message -> System.out.println("[LOG] " + message);
logger.accept("Application started.");
logger.accept("Processing user request.");
logger.accept("Application terminated.");
}
}
Summary
- Use
Consumer<T>
to perform operations on a single input argument. - It can be implemented using lambdas, method references, or anonymous classes.
- It is often used with the
forEach
method of collections or in places where side effects (like logging or output) are important.
Latest posts by Wayan (see all)
- How do I secure servlets with declarative security in web.xml - April 24, 2025
- How do I handle file uploads using Jakarta Servlet 6.0+? - April 23, 2025
- How do I serve static files through a Jakarta Servlet? - April 23, 2025