Java 8 - BiPredicate Example

BiPredicate is a functional interface that takes two arguments and returns a boolean.

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);

    ....
}

Example 1 : test()

BiPredicate object can be implemented with Lambda, and if two arguments are passed to test(), a boolean is returned.

import java.util.function.BiPredicate;

public class BiPredicateExample1 {

    public static void main(String[] args) {

        BiPredicate<Integer, Integer> biPredicate = (n1, n2) -> n1 > n2;

        boolean result = biPredicate.test(10, 100);

        System.out.println(result);
    }
}

Output:

false

Example 2 : and()

and() is a method that chains BiPredicates. AND the results of BiPredicates and return the result.

In the example below, biPredicate1.and(biPredicate2).test(11, 10) returns true when both biPredicate1 and biPredicate2 are true. If either of them is false, false is returned.

import java.util.function.BiPredicate;

public class BiPredicateExample2 {

    public static void main(String[] args) {

        BiPredicate<Integer, Integer> biPredicate1 = (n1, n2) -> n1 > n2;
        BiPredicate<Integer, Integer> biPredicate2 = (n1, n2) -> n1 * n2 > 100;

        boolean result = biPredicate1.and(biPredicate2).test(11, 10);

        System.out.println(result);
    }
}

Output:

true

Example 3 : or()

or() is a method that connects BiPredicates like and(). However, or() returns the result of the OR operation of BiPredicates.

In the example below, biPredicate1.or(biPredicate2).test(10, 9) returns True if any one of biPredicate1 and biPredicate2 becomes True.

import java.util.function.BiPredicate;

public class BiPredicateExample3 {

    public static void main(String[] args) {

        BiPredicate<Integer, Integer> biPredicate1 = (n1, n2) -> n1 > n2;
        BiPredicate<Integer, Integer> biPredicate2 = (n1, n2) -> n1 * n2 > 100;

        boolean result = biPredicate1.or(biPredicate2).test(10, 9);
        System.out.println(result);

        result = biPredicate1.or(biPredicate2).test(9, 10);
        System.out.println(result);
    }
}

Output:

true
false

Example 4 : negate()

negate() returns a Predicate that returns the opposite of the value returned by BiPredicate. You can think of the logical operator NOT as predicated before BiPredicate. It can be used to create logically opposite BiPredicates.

import java.util.function.BiPredicate;

public class BiPredicateExample4 {

    public static void main(String[] args) {

        BiPredicate<Integer, Integer> biPredicate1 = (n1, n2) -> n1 > n2;
        BiPredicate<Integer, Integer> biPredicate2 = biPredicate1.negate();

        boolean result = biPredicate1.test(10, 9);
        System.out.println(result);

        result = biPredicate2.test(10, 9);
        System.out.println(result);
    }
}

Output:

true
false

References

codechachaCopyright ©2019 codechacha