Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

Java BiConsumer: How to Use Two-Argument Actions

Java’s BiConsumer accepts two arguments and returns no value. See its accept method in practice, use it with Map.forEach, and compose actions with andThen.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

BiConsumer<T, U> is Java’s functional interface for an operation that accepts two inputs and returns no result. Use it when code receives a pair—such as a map key and value—and needs to perform an action rather than calculate a value to return. Its functional method is accept(T t, U u). Oracle documents the interface in the Java SE 24 API.

What BiConsumer represents

BiConsumer<T, U> is the two-input counterpart to Consumer<T>. The type parameters describe the first and second argument; the method returns void. In other words, it performs an operation—typically one with a side effect—rather than producing a value for its caller.

As an Amazon Associate I earn from qualifying purchases.

BiConsumer<String, Integer> showPair =
    (name, count) -> System.out.println(name + ": " + count);

showPair.accept("apples", 3);

A lambda works when the surrounding API expects a BiConsumer. You can also implement the interface in a class or pass a compatible method reference. The interface has been part of Java since Java 8, according to its API documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use BiConsumer with Map.forEach

Map.forEach is a direct JDK example: it passes each map entry’s key and value to the supplied action. The API accepts a BiConsumer parameterized with the map’s key and value types.

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);

For a map with a specified type, the same pattern can be written inline:

Map<String, Integer> counts = Map.of("apples", 3, "pears", 5);
counts.forEach((key, value) -> System.out.println(key + "=" + value));

Map.forEach processes entries until they have all been processed or the action throws an exception. Do not assume a universal iteration order: some map implementations define an order, while others, including HashMap, do not. See Oracle’s Java SE 24 Map API.

Chain actions with andThen

andThen creates a composed action: the original BiConsumer runs first, followed by the action passed to andThen.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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, the second is skipped. If either action throws, the exception is relayed to the caller. Passing null as the action to andThen throws NullPointerException. These behaviors are specified in Oracle’s BiConsumer API documentation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose the interface by its inputs and result

Interface Inputs Result Use it when
Consumer<T> One None (void) An action operates on one item.
BiConsumer<T, U> Two None (void) An action operates on a pair.
BiFunction<T, U, R> Two A result of type R A calculation needs to produce a value from a pair.

The Map API makes the distinction concrete: forEach uses a BiConsumer to act on each key-value pair, while replaceAll uses a BiFunction because it needs a result to use as the replacement value. Choose based on what the calling API needs: an effect or a returned answer.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.