Java Lambda - BiPredicate example








BiPredicate represents a predicate which is a boolean-valued function of two arguments.

Method

  1. BiPredicate test
  2. BiPredicate and
  3. BiPredicate negate
  4. BiPredicate or




Example

The following example shows how to use BiPredicate.

import java.util.function.BiPredicate;

public class Main {
  public static void main(String[] args) {
    BiPredicate<Integer, Integer> bi = (x, y) -> x > y;
    System.out.println(bi.test(2, 3));
  }
}

The code above generates the following result.





Example 2

The following code shows how to use BiPredicate as function parameter.

import java.util.function.BiPredicate;
public class Main {
//from ww w  .  ja  va 2  s . c  om
  public static void main(String[] args) {
    boolean result = compare((a, b) -> a / 2 == b, 10, 5);

    System.out.println("Compare result: " + result);

  }
  public static boolean compare(BiPredicate<Integer, Integer> bi, Integer i1, Integer i2) {
    return bi.test(i1, i2);
  }
 
}

The code above generates the following result.