Java Stream How to - Compare two value with BiPredicate








Question

We would like to know how to compare two value with BiPredicate.

Answer

import java.util.function.BiPredicate;
//from w w  w .j av  a  2  s. com
public class Main {

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

  public static void main(String[] args) {

    // i1 -> 10
    // i2 -> 5
    // true if a/2 == b
    boolean result = compare((a, b) -> a / 2 == b, 10, 5);

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

The code above generates the following result.