iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
BiConsumer<T, U> is Java’s standard functional interface for an operation that accepts two inputs and returns no result. Use it when an API gives your code a pair of values and you need to perform an action—such as logging or updating state—rather than calculate a value to return.
What BiConsumer represents
Oracle’s Java SE 24 API defines BiConsumer as an operation that accepts two input arguments and returns no result. Its functional method is void accept(T t, U u): the first argument has type T, the second has type U, and there is no return value. The interface has been part of Java since Java 8. Oracle: BiConsumer (Java SE 24)
Because the result is void, a BiConsumer is suited to an effect, such as printing, recording, or updating something outside the operation. It is not the right shape for a two-input calculation whose answer must be returned.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Use BiConsumer with Map.forEach
Map.forEach passes each map key and its associated value to an action. That makes it a direct fit for a BiConsumer whose type parameters match the map’s key and value types. Oracle: Map (Java SE 24)
import java.util.Map;
import java.util.function.BiConsumer;
BiConsumer<String, Integer> printEntry =
(key, value) -> System.out.println(key + "=" + value);
Map.of("apples", 3, "pears", 5).forEach(printEntry);
The lambda supplies the implementation of accept; the map invokes it with each key-value pair. The order of processing depends on the map implementation: some implementations define an order, while others, including HashMap, do not promise one.
Chain actions with andThen
andThen composes two BiConsumer actions with matching input types. The action on which you call the method runs first, followed by the supplied action.
Rank #2
BiConsumer<String, Integer> logEntry =
(key, value) -> System.out.println("entry: " + key + "=" + value);
BiConsumer<String, Integer> auditEntry =
(key, value) -> audit(key, value);
Map<String, Integer> counts = Map.of("apples", 3, "pears", 5);
counts.forEach(logEntry.andThen(auditEntry));
If the first action throws an exception, the second action is skipped. If either action throws, the exception is relayed to the caller. Passing null as the action to andThen causes a NullPointerException. Oracle: BiConsumer (Java SE 24)
Free tools Windows power users keep installed
One-click scans. No signup required.
Choose the interface by its inputs and result
| Interface | Inputs | Result | Typical use |
|---|---|---|---|
Consumer<T> |
One | None (void) |
Perform an action on one item |
BiConsumer<T, U> |
Two | None (void) |
Perform an action on a pair |
BiFunction<T, U, R> |
Two | A value of type R |
Calculate a result from a pair |
The distinction appears in the map API: forEach takes a BiConsumer to act on each key and value, while replaceAll takes a BiFunction because its result supplies the replacement value. Oracle: Map (Java SE 24)
Quick Recap
Best Value
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

