Javatpoint Logo
Javatpoint Logo

BiPredicate in Java 8

Java 8 brought significant enhancements to the language, including the introduction of functional programming constructs like lambda expressions and the Stream API. Among these additions, the BiPredicate interface stands out as a powerful tool for working with two inputs and making conditional checks. In this article, we will explore BiPredicate in Java 8 in detail, complete with code examples and explanations.

Understanding BiPredicate

In Java, a Predicate is a functional interface that represents a boolean-valued function that takes one argument. Similarly, a BiPredicate is an interface that extends Predicate to handle two input arguments. It is defined as follows:

Here, T and U are the types of the two input arguments, and the test method returns a boolean value based on some condition applied to these inputs.

Creating BiPredicate Instances

You can create BiPredicate instances using lambda expressions or method references. Let's explore both approaches with examples.

Lambda Expression

In this example, we've created a BiPredicate named isSumEven that checks if the sum of two integers is even.

Method Reference

Bipredicate.java

Output:

Are the string lengths equal? True

Using BiPredicate

BiPredicate is often used in conjunction with the Stream API, especially in scenarios where you need to filter or manipulate data based on conditions involving two input elements. Let's look at some practical examples.

BiPredicateExample.java

Output:

Is the sum even? False

Combining BiPredicates

In this example, we combine two BiPredicates using logical operators (and, or, negate):

BiPredicateExample.java

Output:

Is both even and sum greater than 10? false
Is both even and sum greater than 10? false

Filtering a List using BiPredicate

This example shows how to filter a list of names using a BiPredicate:

Output:

Names with length greater than 5 characters: [Charlie]

The BiPredicate interface in Java 8 provides the following methods:

1. test(T t, U u)

This method is the core of the BiPredicate interface. It takes two arguments of types T and U, and it returns a boolean value based on some condition applied to these inputs.

2. Negate() Method

The negate method is used to create a new BiPredicate by negating the current BiPredicate. In other words, it reverses the test condition. If the original BiPredicate returns true, the negated BiPredicate will return false, and vice versa.

3. or() Method

The or method is used to create a new BiPredicate by applying the logical "OR" operation between the current BiPredicate and another BiPredicate. The resulting BiPredicate returns true if at least one of the conditions is true.

4. and() Method

The and method is used to create a new BiPredicate by applying the logical "AND" operation between the current BiPredicate and another BiPredicate. The resulting BiPredicate returns true if both conditions are true.

BiPredicateMethodsExample.java

Output:

Is both even and sum greater than 10? false
Is either even or sum greater than 10? true
Is not even? true

Chaining and Combining Predicates

One of the primary advantages of the negate, or, and methods is the ability to chain and combine multiple predicates together to create complex conditions. This is especially useful when dealing with advanced filtering and validation logic.

For example, you can create a complex BiPredicate by chaining multiple and or operations:

In this example, complexCondition is a combination of isEven and isGreaterThanTen with an additional condition that checks if num1 is equal to 5.

Short-Circuiting Evaluation

The BiPredicate methods (and, or, negate) support short-circuiting evaluation. This means that the evaluation of the conditions stops as soon as the result can be determined.

For and, if the first predicate evaluates to false, there's no need to evaluate the second predicate because the overall result will always be false.

For or, if the first predicate evaluates to true, there's no need to evaluate the second predicate because the overall result will always be true.

This behavior can improve performance and efficiency, especially when dealing with complex predicates.

Use Cases

These methods are valuable in various use cases:

  1. Validation: You can use and to combine multiple validation conditions. For example, validating that a person's age is between 18 and 60 can be expressed as age -> age >= 18 && age <= 60.
  2. Filtering: When working with collections and streams, you can use filter with BiPredicate to filter elements based on complex conditions.
  3. Dynamic Conditions: These methods allow you to dynamically build and modify conditions at runtime, making your code more flexible.

Functional Composition

The ability to chain and combine predicates is a form of functional composition, which is a fundamental concept in functional programming. Functional composition allows you to create complex functions or predicates by composing simpler ones, promoting code reuse and maintainability.

Conclusion

In Java 8, the BiPredicate interface adds a powerful dimension to handling conditions involving two input arguments. Whether you're filtering lists, combining conditions, or performing other complex operations, BiPredicate empowers you with the ability to express your logic concisely and effectively. Embrace this functional programming feature to write more expressive and readable Java code, taking full advantage of Java 8's capabilities.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA