Course contentsShow
← Java
Lesson 1673 of 2,87035. Functional InterfacesFree lesson

Understanding Binary Functional Interfaces

Learn why binary variants exist and how they differ from single-parameter functional interfaces.

Understanding Binary Functional Interfaces

What you'll learn: Why Java provides "binary" variants of functional interfaces and how they differ from their single-parameter cousins.

Why Binary Variants Exist

You've already encountered functional interfaces like Predicate<T>, Function<T, R>, and Consumer<T> — each accepting a single argument. But real-world operations often need two inputs: comparing two values, combining two objects, or processing key-value pairs.

Rather than force you to create custom interfaces for every two-argument scenario, Java provides binary variants of the core functional interfaces.

The Binary Family

Think of binary functional interfaces as the "two-argument versions" of interfaces you already know:

  • BiPredicate<T, U> — tests a condition on two arguments (returns boolean)
  • BiFunction<T, U, R> — transforms two inputs into one output
  • BiConsumer<T, U> — performs an operation on two arguments (returns nothing)

Notice the pattern: add "Bi" prefix, add one more type parameter.

Key Differences

Single-parameter interfaces work with one input:

Function<String, Integer>     // String → Integer
Consumer<String>               // String → void
Predicate<Integer>             // Integer → boolean

Binary interfaces work with two inputs:

BiFunction<String, Integer, Double>  // (String, Integer) → Double
BiConsumer<String, Integer>          // (String, Integer) → void
BiPredicate<String, Integer>         // (String, Integer) → boolean

The method names remain consistent: test() for predicates, apply() for functions, accept() for consumers.

When to Use Them

Use binary variants when your operation naturally involves two distinct inputs — like processing map entries, comparing pairs, or merging objects.

Key Takeaway: Binary functional interfaces extend the standard single-parameter interfaces to handle two-argument operations, following the same naming and method conventions with a "Bi" prefix and an extra type parameter.