Let’s dive into BiPredicate<T, U>, a functional interface used to evaluate a boolean condition based on two inputs.
✅ BiPredicate<T, U>
@FunctionalInterface
public interface BiPredicate<T, U> {
boolean test(T t, U u);
}
Takes two arguments of types T and U
Returns a boolean
It’s like Predicate<T>, but with two inputs
BiPredicate<String, Integer> isLongerThan = (str, length) -> str.length() > length;
System.out.println(isLongerThan.test("banana", 5)); // true
System.out.println(isLongerThan.test("cat", 5)); // false
🔧 Real Use Case: Validation Logic
You might use it to validate input pairs:
BiPredicate<String, String> isLoginMatch = (username, password) ->
"admin".equals(username) && "1234".equals(password);
System.out.println(isLoginMatch.test("admin", "1234")); // true
🔗 Combining with .and(), .or(), .negate()
Just like Predicate, BiPredicate supports chaining:
BiPredicate<Integer, Integer> bothPositive = (a, b) -> a > 0 && b > 0;
BiPredicate<Integer, Integer> sameParity = (a, b) -> (a % 2) == (b % 2);
BiPredicate<Integer, Integer> combined = bothPositive.and(sameParity);
System.out.println(combined.test(4, 2)); // true
System.out.println(combined.test(4, -2)); // false (not both positive)
✅ Summary
| Interface | Input Types | Return Type | Use Case Example |
|---|---|---|---|
BiPredicate<T,U> | T, U | boolean | (s, n) -> s.length() > n, login validation |