Java 8 Streams API is very robust and feature-rich API. It provides multiple ways to write beautiful and fewer lines of code. In that series, there is an interface Predicate Interface. Let’s start understanding Java Predicate Interface.
1. What is Java 8 Predicate and Understanding Java Predicate Interface.
Java 8 Predicate Interface is a functional interface, By functional interface you know it will have only one abstract method and the programmer writes the body for that one abstract method as part of Lambda expression.
Predicate is a type of functional interface which takes a parameter and return a boolean result. Let’s look at the Predicate Interface.
Here as you can see it has an abstract method “test” which accepts a parameter and returns a boolean value either true or false. Let’s see some example.
The above filter example takes Predicate and filters the values based on predicates result. If the result is true it keeps the employee and if the result is false it discards the employee. In the end, the collect method collects it and form another list. Predicate is used with the Optional class as well.
2. Chaining Predicate interfaces.
Here are the other important methods from Predicate interface.
Predicate interface contains three more methods for logical chaining of multiple Predicates.
2.1 Chain Predicate using ‘And’ condition.
This example shows how two predicates are joined using and condition.
2.2 Chain Predicate using ‘Or’ condition.
This example shows how two predicates are joined using ‘or’ condition.
2.3 Negate Predicate.
This example shows how to negate predicates. Turn true to false and vice versa.
In this blog, we discussed how to use Java 8 Predicate interface and it’s important method. Later we discussed on chaining of the Predicate interface. That’s it on Understanding Java Predicate Interface. You can further read about Functional Interfaces.