Posts

Showing posts with the label Predicate

Predicate interface in Java 8 with examples

In this blog post, I will be explaining how the Java 8 functional interface Predicate works. To know more about functional interfaces, you can refer this blog post. Edit The Predicate interface provides a method called test. This method accepts a parameter of any data type and returns a boolean. Predicate method with Integer argument Consider the following code snippet: public class PredicateDemo {public static void main(String[] args) {Predicate<Integer> greaterThan8 = (input) -> input > 8;System.out.println("4 is greater than 8 = "+greaterThan8.test(4));System.out.println("12 is greater than 8 = "+greaterThan8.test(12));}} Here, the Predicate.test method checks if the input number is greater than 8. So when the above code is executed, it will print the following output: 4 is greater than 8 = false12 is greater than 8 = true Predicate method with String argument Consider the following code snippet: import java.util.function.Predicate;public class...