In this article, we will discuss pre-defined Functional Interface in Java 1.8 version for conditional checks which accepts 2 input arguments for evaluation i.e.; BiPredicate Functional Interface
1. BiPredicate Functional Interface:
- This Functional Interface has one abstract method called test(T t, U u); which
- Step 1 :- accepts 2 input argument of any data-type
- Step 2 :- performs some conditional checks to evaluate result
- Step 3 :- And always return result as Boolean value either as true/false
- Below is interface BiPredicate containing test(); method along with other default & static methods
- Note: this BiPredicate Functional Interface is exactly same as Predicate except that it accepts 2 input arguments
package java.util.function; import java.util.Objects; @FunctionalInterface public interface BiPredicate<T, U> { /** * Evaluates this predicate on the given arguments. * * @param t the first input argument * @param u the second input argument * @return {@code true} if the input arguments match the predicate, * otherwise {@code false} */ boolean test(T t, U u); // other default and static methods }
2. Examples for BiPredicate Functional Interface:
Example 1 – program to evaluate whether sum of given 2 number is odd or even
package net.bench.resources.bipredicate.example; import java.util.function.BiPredicate; public class TestEvenOddUsingBiPredicate { public static void main(String[] args) { // lambda expression to evaluate sum is EVEN / ODD using BiPredicate FI BiPredicate<Integer, Integer> bp = (i,j) -> (i+j)%2 == 0; // testing sum using BiPredicate FI System.out.println("1. Whether sum of (10, 6) is even/odd \t: " + bp.test(10, 6)); System.out.println("2. Whether sum of (20, 5) is even/odd \t: " + bp.test(20, 5)); System.out.println("3. Whether sum of (7, 3) is even/odd \t: " + bp.test(7, 3)); System.out.println("4. Whether sum of (15, 30) is even/odd \t: " + bp.test(15, 30)); System.out.println("5. Whether sum of (25, 75) is even/odd \t: " + bp.test(25, 75)); } }
Output:
1. Whether sum of (10, 6) is even/odd : true 2. Whether sum of (20, 5) is even/odd : false 3. Whether sum of (7, 3) is even/odd : true 4. Whether sum of (15, 30) is even/odd : false 5. Whether sum of (25, 75) is even/odd : true
Example 2 – program to evaluate whether 2nd number is divisor of 1st number
package net.bench.resources.bipredicate.example; import java.util.function.BiPredicate; public class CheckDivisorUsingBiPredicate { public static void main(String[] args) { // lambda expression to evaluate sum is EVEN / ODD using BiPredicate FI BiPredicate<Integer, Integer> bp = (i,j) -> i%j == 0; // testing sum using BiPredicate FI System.out.println("1. Whether 2 divisor of 10 : " + bp.test(10, 2)); System.out.println("2. Whether 5 divisor of 25 : " + bp.test(25, 5)); System.out.println("3. Whether 3 divisor of 38 : " + bp.test(38, 3)); System.out.println("4. Whether 7 divisor of 80 : " + bp.test(80, 7)); System.out.println("5. Whether 1 divisor of 17 : " + bp.test(17, 1)); } }
Output:
1. Whether 2 divisor of 10 : true 2. Whether 5 divisor of 25 : true 3. Whether 3 divisor of 38 : false 4. Whether 7 divisor of 80 : false 5. Whether 1 divisor of 17 : true
3. Difference between Predicate and BiPredicate :
- Predicate :- It accepts just 1 input argument and does conditional check on these input and returns either true/false based on the operation implemented in the lambda expression
- BiPredicate :- Whereas BiPredicate accepts 2 input arguments and does conditional check on these inputs and returns either true/false based on the operation implemented in the lambda expression
- So, it is very simple if we want to perform conditional check on single argument then go for Predicate
- But in case if we require to perform conditional checks on 2 input arguments then BiPredicate is the best option
package net.bench.resources.bipredicate.example; import java.util.function.BiPredicate; import java.util.function.Predicate; public class DifferenceInPredicateAndBiPredicate { public static void main(String[] args) { // 1. Predicate - lambda expression to check number is greater than 10 Predicate<Integer> p = (i) -> i > 10; // 2. BiPredicate - lambda expression to check sum is greater than 10 BiPredicate<Integer, Integer> bp = (i,j) -> (i+j) > 10; // 1.A test for Predicate System.out.println("Predicate testing : \n"); System.out.println("1. Whether 8 is greater than 10 \t: " + p.test(8)); System.out.println("2. Whether 19 is greater than 10 \t: " + p.test(19)); // 2.A test for BiPredicate System.out.println("\nBiPredicate testing : \n"); System.out.println("1. Whether sum of (10, 6) is greater than 10 \t: " + bp.test(10, 6)); System.out.println("2. Whether sum of (7, 3) is greater than 10 \t: " + bp.test(7, 3)); } }
Output:
Predicate testing : 1. Whether 8 is greater than 10 : false 2. Whether 19 is greater than 10 : true BiPredicate testing : 1. Whether sum of (10, 6) is greater than 10 : true 2. Whether sum of (7, 3) is greater than 10 : false
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/function/BiPredicate.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
- https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
Happy Coding !!
Happy Learning !!