Java Code Example: predicate

In this example, we define a lambda expression n -> n % 2 == 0 that checks if a number is even. We assign this lambda expression to a Predicate<Integer> functional interface, which represents a function that takes an Integer argument and returns a boolean. We then call the test method on the isEven object to check if the number 10 is even.

Predicate<Integer> isEven = (n) -> n % 2 == 0;
boolean result = isEven.test(10); // true