In Java 8, BiPredicate represents a Java Predicate (boolean-valued function) of two arguments and returns boolean
value. This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. BiPredicate functional method is test(Object, Object)
1. BiPredicate example
Following program is demonstrates a simple example on how to use a BiPredicate in Java 8.
public class BiPredicateTest { public static void main(String[] args) { // A basic BiPredicate usage BiPredicate<Integer, Integer> p = (n1, n2) -> n1 > n2; System.out.println(p.test(20, 10));// true System.out.println(p.test(12, 24));// false } }
Output :
true false
2. Java BiPredicate methods
boolean test(T t, U u)
– Evaluates this predicate on the given arguments.default BiPredicate or(BiPredicate other)
– Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.default BiPredicate negate()
– Returns a predicate that represents the logical negation of this predicate.default BiPredicate and(BiPredicate other)
– Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another
2.1. BiPredicate and() example
The following basic example demonstrates how BiPredicate and()
method works.
public class BiPredicateAndTest { public static void main(String[] args) { BiPredicate<Integer, Integer> bp1 = (n1, n2) -> (n1 % n2 == 0); BiPredicate<Integer, Integer> bp2 = (n1, n2) -> (n1 * n2 > 100); // n1 should divisible by n2 and n1*n2 greater than 100 System.out.println(bp1.and(bp2).test(120, 6)); // flase BiPredicate<String, String> bp3 = (s1, s2) -> s1.startsWith(s2); BiPredicate<String, Integer> bp4 = (s1, s2) -> s1.length() > s2; BiPredicate<String, String> bp5 = (s1, s2) -> s1.endsWith(s2); // System.out.println(bp3.and(bp4).test("ANAND", "D")); // incompatible types // s1 should starts with "P" and ends with "P" System.out.println(bp3.and(bp5).test("Peter", "P")); // false // s1 should starts with "A" and end with "A" System.out.println(bp3.and(bp5).test("ASHJA", "A")); // true } }
Output :
true false true
2.2. BiPredicate or() example
The following basic example demonstrates how BiPredicate or()
method works.
public class BiPredicateOrTest { public static void main(String[] args) { BiPredicate<Integer, Integer> bp1 = (n1, n2) -> (n1 % n2 == 0); BiPredicate<Integer, Integer> bp2 = (n1, n2) -> (n1 * n2 > 100); // n1 should be divisible by n2 or greater than 100 System.out.println(bp1.or(bp2).test(120, 7)); // true System.out.println(bp1.or(bp2).test(13, 5)); // false BiPredicate<String, String> bp3 = (s1, s2) -> s1.startsWith(s2); BiPredicate<String, Integer> bp4 = (s1, s2) -> s1.length() > 5; BiPredicate<String, String> bp5 = (s1, s2) -> s1.endsWith(s2); // name starts with "A" or not ends with "p" // System.out.println(bp3.or(bp4)); // bp3 and bp4 are incompatible types System.out.println(bp3.or(bp5).test("Peter", "r")); // true System.out.println(bp3.or(bp5).test("Anand", "A")); // true } }
Output :
true false true true
1.3. BiPredicate negate() example
The following basic example demonstrates how BiPredicate negate()
method works.
public class BiPredicateNegateTest { public static void main(String[] args) { BiPredicate<Integer, Integer> bp1 = (n1, n2) -> (n1 % n2 == 0); BiPredicate<Integer, Integer> bp2 = (n1, n2) -> (n1 * n2 > 100); // n1 should not be divisible by n2 and n1*n2 not greater than 100 System.out.println(bp1.negate().test(120, 6)); // flase System.out.println(bp2.negate().test(12, 7)); // true BiPredicate<String, String> bp3 = (s1, s2) -> s1.startsWith(s2); BiPredicate<String, Integer> bp4 = (s1, s2) -> s1.length() > s2; BiPredicate<String, String> bp5 = (s1, s2) -> s1.endsWith(s2); // s1 should not starts with s2 System.out.println(bp3.negate().test("ANAND", "D")); // true // s1 lengthe should not greater than s2 System.out.println(bp4.negate().test("PETER", 5)); // true // s1 should not ends with s2 System.out.println(bp5.negate().test("ASHJA", "A")); // false } }
false true true true false
3. Conclusion
BiPredicate is a built-in java functional interface which takes two arguments and return a boolean
value. In this article we have covered about Java BiPredicate interface and methods of it with basic examples.