Java.util.function.BiPredicate interface in Java with Examples
The BiPredicate<T, V> interface was introduced in JDK 8. This interface is packaged in java.util.function package. It operates on two objects and returns a predicate value based on that condition. It is a functional interface and thus can be used in lambda expression also.
public interface BiPredicate<T, V>
Methods:
- test(): This function evaluates a conditional check on the two objects, and returns a boolean value denoting the outcome.
boolean test(T obj, V obj1)
- and(): This function applies the AND operation on the current object and the object received as argument, and returns the newly formed predicate. This method has a default implementation.
default BiPredicate<T, V> and(BiPredicate<? super T, ? super V> other)
- negate(): This function returns the inverse of the current predicate i.e reverses the test condition. This method has a default implementation.
default BiPredicate<T, V> negate()
- or(): This function applies the OR operation on the current object and the object received as argument, and returns the newly formed predicate. This method has a default implementation.
default BiPredicate<T, V> or(BiPredicate<? super T, ? super V> other)
Example:
Java
// Java example to demonstrate BiPredicate interfaceimportjava.util.function.BiPredicate;publicclassBiPredicateDemo {publicstaticvoidmain(String[] args){// Simple predicate for checking equalityBiPredicate<Integer, String> biPredicate = (n, s) ->{if(n == Integer.parseInt(s))returntrue;returnfalse;};System.out.println(biPredicate.test(2,"2"));// Predicate for checking greater thanBiPredicate<Integer, String> biPredicate1 = (n, s) ->{if(n > Integer.parseInt(s))returntrue;returnfalse;};// ANDing the two predicatesBiPredicate<Integer, String> biPredicate2= biPredicate.and(biPredicate1);System.out.println(biPredicate2.test(2,"3"));// ORing the two predicatesbiPredicate2 = biPredicate.or(biPredicate1);System.out.println(biPredicate2.test(3,"2"));// Negating the predicatebiPredicate2 = biPredicate.negate();System.out.println(biPredicate2.test(3,"2"));}}Output:true false true true
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/function/BiPredicate.html



