# What is the BiPredicate in Java 8

Normal **Predicate** predefined functional interface that accepts only one input and returns a boolean result. But if we provide two inputs to the Predicate, it is not accepted. That’s why Java 8 introduced a BiPredicate predefined functional interface. It accepts two inputs and returns a boolean result. This predefined functional interface is available in the **java.util.function** package.

**Prototype:**

```
interface BiPredicate<T, U>{
public boolean test(T t, U u);
}
```

**Type Parameters:**

T - It is the type of the first argument to the predicate

U - It is the type of the second argument the predicate

**BiPredicate Example:**

```
import java.util.function.BiPredicate;
public class Test {
public static void main(String[] args) {
BiPredicate<String, Integer> bi = (a, b) -> {
return a.length() == b;
};
boolean result = bi.test("Peter", 5);
System.out.println(result);
boolean result1 = bi.test("Robert", 4);
System.out.println(result1);
boolean result2 = bi.test("William", 7);
System.out.println(result2);
}
}
```

**Output:**

```
true
false
true
```

**BiPredicate Methods:**

• boolean test(T t, U u): This method evaluates this predicate on the given arguments

• default BiPredicate or(BiPredicate other): This method returns a composed predicate that represents a short-circuiting logical OR of this predicate and another

• default BiPredicate and(BiPredicate other): This method returns a composed predicate that represents a short-circuiting logical AND of this predicate and another

• default BiPredicate negate(): This method returns a predicate that represents the logical negation of this predicate

**BiPredicate and() example:**

```
import java.util.function.BiPredicate;
public class Test {
public static void main(String[] args) {
BiPredicate<Integer, Integer> bi1 = (a, b) -> a%b==0;
BiPredicate<Integer, Integer> bi2 = (a, b) -> a*b<50;
System.out.println("The a is divisible by b and a*b is less than 50: "+bi1.and(bi2).test(10, 2));
System.out.println("The a is divisible by b and a*b is less than 50: "+bi1.and(bi2).test(30, 9));
System.out.println("The a is divisible by b and a*b is less than 50: "+bi1.and(bi2).test(50, 2));
}
}
```

**Output: **

```
The a is divisible by b and a*b is less than 50: true
The a is divisible by b and a*b is less than 50: false
The a is divisible by b and a*b is less than 50: false
```

**BiPredicate or() example:**

```
import java.util.function.BiPredicate;
public class Test {
public static void main(String[] args) {
BiPredicate<Integer, Integer> bi1 = (a, b) -> a%b==0;
BiPredicate<Integer, Integer> bi2 = (a, b) -> a*b<50;
System.out.println("The a is divisible by b or a*b is less than 50: "+bi1.or(bi2).test(10, 2));
System.out.println("The a is divisible by b or a*b is less than 50: "+bi1.or(bi2).test(30, 9));
System.out.println("The a is divisible by b or a*b is less than 50: "+bi1.or(bi2).test(50, 2));
}
}
```

**Output:**

```
The a is divisible by b or a*b is less than 50: true
The a is divisible by b or a*b is less than 50: false
The a is divisible by b or a*b is less than 50: true
```

**BiPredicate negate() example:**

```
import java.util.function.BiPredicate;
public class Test {
public static void main(String[] args) {
BiPredicate<Integer, Integer> bi1 = (a, b) -> a%b==0;
BiPredicate<Integer, Integer> bi2 = (a, b) -> a*b<50;
System.out.println("The a is not divisible by b or a*b is not less than 50: "+bi1.negate().test(10, 2));
System.out.println("The a is not divisible by b or a*b is not less than 50: "+bi2.negate().test(30, 9));
System.out.println("The a is not divisible by b or a*b is not less than 50: "+bi1.negate().test(50, 2));
}
}
```

**Output:**

```
The a is not divisible by b or a*b is not less than 50: false
The a is not divisible by b or a*b is not less than 50: true
The a is not divisible by b or a*b is not less than 50: false
```

**Conclusion:**

This topic is explained **What is a BiPredicate? What are the methods of it? How to use it?**

#### Leave your thought here

Your email address will not be published. Required fields are marked *