Java BiPredicate

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.

🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.

▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube

Introduction

In Java, the BiPredicate interface is a functional interface that represents a predicate (boolean-valued function) with two arguments. It is part of the java.util.function package and is used for testing conditions involving two parameters.

Table of Contents

  1. What is BiPredicate?
  2. Methods and Syntax
  3. Examples of BiPredicate
  4. Real-World Use Case
  5. Conclusion

1. What is BiPredicate?

BiPredicate is a functional interface that accepts two arguments and returns a boolean result. It is commonly used in lambda expressions and method references for evaluating conditions.

2. Methods and Syntax

The main method in the BiPredicate interface is:

  • boolean test(T t, U u): Evaluates this predicate on the given arguments.

Syntax

BiPredicate<T, U> biPredicate = (T t, U u) -> {
    // condition on t and u
    return result;
};

3. Examples of BiPredicate

Example 1: Checking if Two Strings are Equal

import java.util.function.BiPredicate;

public class BiPredicateExample {
    public static void main(String[] args) {
        // Define a BiPredicate that checks if two strings are equal
        BiPredicate<String, String> areEqual = (str1, str2) -> str1.equals(str2);

        boolean result = areEqual.test("hello", "hello");

        System.out.println("Are equal: " + result);
    }
}

Output:

Are equal: true

Example 2: Checking if One Number is Greater than Another

import java.util.function.BiPredicate;

public class GreaterThanExample {
    public static void main(String[] args) {
        // Define a BiPredicate that checks if the first number is greater than the second
        BiPredicate<Integer, Integer> isGreater = (a, b) -> a > b;

        boolean result = isGreater.test(10, 5);

        System.out.println("Is greater: " + result);
    }
}

Output:

Is greater: true

4. Real-World Use Case: Validating User Credentials

In authentication systems, BiPredicate can be used to validate user credentials, such as checking if a username and password match.

import java.util.function.BiPredicate;

public class UserValidation {
    public static void main(String[] args) {
        // Define a BiPredicate to validate username and password
        BiPredicate<String, String> validateCredentials = (username, password) ->
            "user123".equals(username) && "pass123".equals(password);

        boolean isValid = validateCredentials.test("user123", "pass123");

        System.out.println("Credentials valid: " + isValid);
    }
}

Output:

Credentials valid: true

Conclusion

The BiPredicate interface is used in Java for evaluating conditions involving two parameters. It simplifies writing boolean logic in functional programming and is particularly useful in scenarios like data validation, filtering, and comparison operations. Using BiPredicate can enhance code readability and maintainability.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare