In Java 8, there is a defined functional interface which is called Predicate. The Predicate interface receives an argument and based on specific condition it returns Boolean value. It can be helpful in testing and it’s located in java.util.Function package. This is one of the many pre-defined or built-in functional interface which JDK provides. A couple of important ones are Supplier which can be used to produce a value of T (any object) and Consumer which can consume and project like takes data and print to console. forEach() is a good example of method accepting Consumer interface. Similarly many method accept a Predicate and when they do you can simply pass a lambda which generates a boolean by checking a condition.
0 Comments