BiPredicate Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expect two parameters as an input and produces a boolean value as an output. As it is a functional interface, it can be used assignment target for lambda expression or method reference. In this post, we are going to see several implementations of BiPredicate Interface by using different examples.
BiPredicate Interface in Java 8 with Examples...!!! Share on X
Look at BiPredicate Javadoc description below:
BiPredicate Interface contains 4 methods:
test
and
or
negate
Let’s discuss these methods:
test
This method performs operation on the given two arguments and results in a boolean value.
1 | boolean test(T t, U u); |
and
This method returns a composed predicate that represents a short-circuiting logical AND of this predicate and another. When evaluating the composed
predicate, if this predicate is false, then the other predicate is not evaluated.
1 2 3 4 | default BiPredicate<T, U> and(BiPredicate<? super T, ? super U> other) { Objects.requireNonNull(other); return (T t, U u) -> test(t, u) && other.test(t, u); } |
or
This method returns a composed predicate that represents a short-circuiting logical OR of this predicate and another. When evaluating the composed predicate, if this predicate is true, then the other predicate is not evaluated.
1 2 3 4 | default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> other) { Objects.requireNonNull(other); return (T t, U u) -> test(t, u) || other.test(t, u); } |
negate
This method returns a predicate that represents the logical negation of this predicate.
1 2 3 | default BiPredicate<T, U> negate() { return (T t, U u) -> !test(t, u); } |
Lets understand above mentioned methods thorough various examples:
Example 1. Simple Predicate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import java.util.function.BiPredicate; public class BiPredicateInterfaceJava8Example1 { public static void main(String[] args) { System.out.println("BiPredicate Interface - Java 8 - Example : Simple BiPredicate"); BiPredicate <Integer, Integer> biPredicateObj = (i1, i2) -> i1 == i2; System.out.println("Are numbers matching: " + biPredicateObj.test(5, 10)); } } |
Example 2. Predicate Chaining with ‘and’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.function.BiPredicate; public class BiPredicateInterfaceJava8Example2 { public static void main(String[] args) { System.out.println("BiPredicate Interface - Java 8 - Example : BiPredicate Chaining"); BiPredicate <Integer, Integer> valuesNotMatching = (i1, i2) -> i1 != i2; BiPredicate <Integer, Integer> valueOneGreaterThanValueTwo = (i1, i2) -> i1 > i2; //BiPredicate chaining : Example with 'and' predicate boolean result = valuesNotMatching.and(valueOneGreaterThanValueTwo).test(5, 10); System.out.println(result); } } |
Example 3. Passing Predicate into a Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.util.function.BiPredicate; public class BiPredicateInterfaceJava8Example3 { public static void main(String[] args) { System.out.println("BiPredicate Interface - Java 8 - Example : Passing BiPredicate into a Function"); BiPredicate <Integer, Integer> biPredicateObj = (i1, i2) -> i1 == i2; checkIfValueMatches(5, biPredicateObj); } private static void checkIfValueMatches(int i, BiPredicate <Integer, Integer> bip) { System.out.println("Does value matching: " + bip.test(i, 10)); } } |
Example 4. ‘or’ with Anonymous/Lambda Predicate Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import java.util.function.BiPredicate; public class BiPredicateInterfaceJava8Example4 { public static void main(String[] args) { System.out.println("BiPredicate Interface - Java 8 - Example : OR & Anonymous/Lambda BiPredicate Implementations"); //BiPredicate anonymous implementation BiPredicate <String, String> startsWithSpecificString = new BiPredicate <String, String> () { @Override public boolean test(String s1, String s2) { return s1.startsWith(s2); } }; //Predicate lambda implementation BiPredicate <String, String> containsSpecificString = (s1, s2) -> s1.contains(s2); //OR condition boolean result = startsWithSpecificString.or(containsSpecificString).test("ish", "isha durani"); System.out.println("Result: " + result); } } |
Example 5. Predicate ‘negate’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.function.BiPredicate; public class BiPredicateInterfaceJava8Example5 { public static void main(String[] args) { BiPredicate <String, String> predicate = (s1, s2) -> s1.equals(s2); //NEGATE operation BiPredicate <String, String> predicateNegate = predicate.negate(); System.out.println(predicateNegate.test("Hello Predicate", "Hello")); } } |
Example 6. Predicate List Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.util.ArrayList; import java.util.List; import java.util.function.BiPredicate; public class BiPredicateInterfaceJava8Example6 { public static void main(String[] args) { System.out.println("BiPredicate Interface - Java 8 - Example : BiPredicate List Example\n"); BiPredicate <Integer, List <Integer>> listPredicateContainsSpecificInteger = (i, l) -> l.contains(i); List <Integer> list = new ArrayList <Integer> (); list.add(19); list.add(4); list.add(83); list.add(5); list.add(63); checkIfValuePresentInList(list, listPredicateContainsSpecificInteger); } public static void checkIfValuePresentInList(List <Integer> list, BiPredicate <Integer, List <Integer>> bip) { if (bip.test(5, list)) { System.out.println("Success: List contains the expected value which is number 5"); } else { System.out.println("Failure: List doens't contains the expected value which is number 5"); } } } |
Example 7. with custom Class object (like ‘Employee’ in this case)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import java.util.function.BiPredicate; public class BiPredicateInterfaceJava8Example7 { public static void main(String[] args) { System.out.println("BiPredicate Interface - Java 8 - Example : BiPredicate Custom Class Example\n"); BiPredicate <Integer, Employees> employeePredicate = (i, emp) -> emp.getAge() > i; Employees e1 = new Employees(15, "Tom"); Employees e2 = new Employees(6, "Sam"); Employees e3 = new Employees(88, "Mark"); System.out.println("Age of First Employee greater than 33? : " + employeePredicate.test(33, e1)); System.out.println("Age of Second Employee greater than 15? : " + employeePredicate.test(15, e2)); System.out.println("Age of Third Employee greater than 45? : " + employeePredicate.test(45, e3)); } } class Employees { int age; String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Employees(int age, String name) { this.age = age; this.name = name; } } |
BiPredicate Interface in Java 8 with Examples...!!! Share on X
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Supplier Interface in Java 8 with Examples