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.
T: type of the first inputU: type of the second inputR: 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.
Rank #2
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:
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.
Rank #4
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.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →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.
Best Value
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.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Which 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.
Quick Recap
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.




