Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

BiFunction

A function of two arguments.

Available since: Java 8

Syntax

@FunctionalInterface
public interface BiFunction<T, U, R> {
    R apply(T t, U u);
}

Examples

A price for a quantity:

void main() {
    BiFunction<Double, Integer, Double> total = (price, qty) -> price * qty;
    IO.println(total.apply(2.5, 4));
}

Output:

10.0

Notes

There are other two-argument interfaces: BiConsumer (used by Map.forEach()) and BiPredicate. If both arguments and the result share a type, BinaryOperator is handier. The standard library has no three-argument functions — declare your own interface.

See also

  • Function — A function that takes a value of type `T` and returns a value of type `R`.
  • BinaryOperator — A two-argument function whose arguments and result share a type.
  • Map.merge() — Adds a value under a key or combines it with the existing one.

In the tutorial

  • Functional interfaces — Functional interfaces in Java: the @FunctionalInterface annotation, the standard Function, Predicate, Consumer, Supplier and combining them.

Thanks for reading!

Donate to the site — we work hard for you.