Java 8 — BiPredicate | Code Factory

Code Factory
1 min readMay 6, 2020

--

Index Page : Link

Reference Link : Link

Donate : Link

Normal Predicate can take only one input argument and perform some conditional check. Sometimes our programming requirement is we have to take 2 input arguments and perform some conditional check, for this requirement we should go for BiPredicate.

BiPredicate is exactly same as Predicate except that it will take 2 input arguments.

@FunctionalInterface
public interface BiPredicate<T,U> {
boolean test(T t, U u);
default BiPredicate<T,U> and(BiPredicate<? super T,? super U> other) { }
default BiPredicate<T,U> negate() { }
default BiPredicate<T,U> or(BiPredicate<? super T,? super U> other) { }
}

To check the sum of 2 given integers is even or not by using BiPredicate :

package com.codeFactory.bipredicate;import java.util.function.BiPredicate;public class Test {
public static void main(String... args) {
BiPredicate<Integer, Integer> p = (a, b) -> ((a+b) % 2 == 0);
System.out.println(p.test(10, 20));
System.out.println(p.test(7, 10));
}
}

Output :

true
false

--

--