October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Java BiFunction Explained: Two Inputs, One Result

Java’s BiFunction takes two typed inputs and returns one result. Learn how apply and andThen work, why BiFunction has no compose method, and how JDK APIs use it.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

BiFunction<T,U,R> takes two values—one of type T and one of type U—and returns a value of type R. Use its apply method to run the operation, and andThen to transform the result. Unlike Function, BiFunction does not provide compose.

What is a BiFunction in Java?

BiFunction<T,U,R> is a standard functional interface for an operation with two inputs and one result. Oracle’s Java SE 26 BiFunction API documentation describes it as: “Represents a function that accepts two arguments and produces a result.” The interface has existed since Java 1.8.

As an Amazon Associate I earn from qualifying purchases.

The type parameters identify the two inputs and the output:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • T: type of the first input
  • U: type of the second input
  • R: type of the returned result

Its single abstract method is R apply(T t, U u). That method makes a BiFunction a target for a lambda expression or a compatible method reference. For example:

BiFunction<Integer, Integer, Integer> add = (left, right) -> left + right;

Integer total = add.apply(3, 4); // 7

The generic types help the compiler check the argument and return types. In this example, both arguments and the result are Integer; they do not have to be the same type in other uses.

How is BiFunction different from Function and BinaryOperator?

Choose the interface that matches the callback’s shape. Function<T,R> takes one argument; BiFunction<T,U,R> takes two; and BinaryOperator<T> is for two arguments of the same type that produce that same type.

Interface Inputs Result Composition method
Function<T,R> One value of type T R compose and andThen
BiFunction<T,U,R> One value of type T and one of type U R andThen
BinaryOperator<T> Two values of type T T Inherited from BiFunction: andThen

BinaryOperator<T> specializes BiFunction<T,T,T>. It is a useful choice when the operation combines two values without changing their type, such as adding two integers or selecting the greater of two values.

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

How do I use BiFunction’s andThen method?

andThen keeps the original two inputs, calls the BiFunction, then passes its result to another function. If the first operation returns R and the next function returns V, the combined operation is a BiFunction<T,U,V>.

BiFunction<Integer, Integer, Integer> add = (left, right) -> left + right;
Function<Integer, String> label = total -> "Total: " + total;
BiFunction<Integer, Integer, String> addAndLabel = add.andThen(label);

String result = addAndLabel.apply(3, 4); // "Total: 7"

The declared method accepts Function<? super R, ? extends V> and returns BiFunction<T,U,V>. The variance bounds allow a function that accepts a supertype of the first operation’s result and returns a subtype of the combined result. The sequence is always: apply the two-input operation, then transform its output.

If either operation throws an exception, it propagates to the caller. Passing null as the after function causes NullPointerException.

Does BiFunction have a compose method?

No. The BiFunction interface provides andThen, but not compose. Its API summary lists no two-input composition method; compose belongs to the one-input Function interface.

To preprocess the inputs, transform them explicitly and then call apply:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Function<String, Integer> parse = Integer::valueOf;
BiFunction<Integer, Integer, Integer> add = (left, right) -> left + right;

String first = "3";
String second = "4";
Integer result = add.apply(parse.apply(first), parse.apply(second)); // 7

This makes the two conversions visible and keeps the resulting operation’s input types clear. For repeated use, a helper method can package the preprocessing and two-input calculation into one named operation.

Where is BiFunction used in Java?

Several JDK APIs accept a two-argument callback, but the callback’s role and the API’s behavior differ. The following are representative uses documented in Java’s standard API references.

Map.compute: calculate a mapping from its key and current value

Map.compute accepts a remapping function that receives the key and its current mapped value and returns a replacement value:

map.compute(key, (existingKey, currentValue) ->
    recompute(existingKey, currentValue));

The remapping callback may receive a null current value in cases specified by the map contract. Consult the contract for the particular map and Java version before relying on null handling or mutation behavior; those details should not be generalized across all map implementations.

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

Map.merge: combine a value with an existing mapping

Map.merge takes a remapping function to combine a supplied value with an existing non-null mapping. This is useful when a map should accumulate values, such as combining counts. Its callback is a two-value operation, but its null and removal rules are defined by merge rather than by BiFunction itself.

Stream.reduce: accumulate a stream with a two-argument operation

One Stream.reduce overload accepts a BiFunction as an accumulator. The callback combines the current accumulated result with the next stream element. Use the overload and accumulator that match the stream’s result and element types, and follow the reduction contract rather than assuming it behaves like a simple sequential loop.

CompletionStage.thenCombine: combine results from two stages

CompletionStage.thenCombine accepts a function that receives the results of two normally completed stages and returns a combined result. The callback runs as part of the stage composition; it is not a general promise that both operations execute in a particular order.

Concurrent map APIs also accept BiFunction callbacks for remapping or combining values. Their concurrency guarantees come from the specific map operation and implementation contract, not from the functional interface.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Which Java version supports BiFunction?

BiFunction has been part of Java since Java 1.8. The current Oracle reference linked above is for Java SE 26. Check the supported Java baseline of your project before using it; code that uses this interface requires Java 8 or later.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

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.