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 (returnsboolean)BiFunction<T, U, R>— transforms two inputs into one outputBiConsumer<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.