This tutorial explains the functional interface Predicate which has been newly introduced in the java.util.function package. It describes Predicate’s usage with the help of multiple examples.
What is java.util.function.Predicate – Predicate is a new functional interfaceClick to read tutorial on Functional Interfaces defined in java.util.function package which can be used in all the contexts where an object needs to be evaluated for a given test condition and a boolean value needs to be returned based on whether the condition was successfully met or not.
Since Predicate is a functional interface, hence it can be used as the assignment target for a lambda expressionClick to read Lambda Expressions tutorial or a method referenceClick to read tutorial on Method References.
Advantage of predefined java.util.function.Predicate – Wherever an object needs to be evaluated and a boolean value needs to be returned( or a boolean-valued Predicate exists – in mathematical terms) the Predicate functional interface can be used. The user need not define his/her own predicate-type functional interface.
boolean test(T t)is the abstract method which will define the signature of the lambda expression/method reference which can be assigned to a target of type
- T is the type of input to the predicate
boolean test(T t)returns true if the input argument matches the predicate(the test condition), otherwise returns false
- There are 3 default methods & 1 static method in Predicate which I will explain in below sections of this tutorial.
Example of using Predicate for a boolean condition check –
- The static method
filterList()takes two inputs –
- A List of Integers which need to be filtered based on some condition
- An instance of Predicate interface which is the condition for evaluation of each integer passed in the Integer list.
filterList()method loops through the whole IntegerList and whichever integer passes the condition test it is added to the resultant list called
- The caller of
filterList()gets a filtered list back which contains all the Integers which satisfy the test condition i.e. greater then zero or positive. This is exactly the output we saw above, i.e. 1 10 200 101
- A lambda expression
i -> i > 0assigned to an instance of type Predicate named positive.
- This predicate instance
positiveis then passed as the second argument of
- The input
Integerlist can thus be filtered in a different way by writing a new lambda with a new test condition(such as less than zero, greater than 100 etc) and the
filterList()method will apply the test condition to the input list passed. Thus, the test condition is passed as a parameter to the
filterList()method using the
Default methods in java.util.function.Predicate: There a few default methods also provided in the Predicate functional interface which enable us to do various types of boolean operations such as and, or, not(negate) with different instances of Predicate. These default methods are –
|Default Method Name||Explanation|
||It does logical AND of the predicate on which it is called with another predicate. Example:
||It does logical OR of the predicate on which it is called with another predicate. Example:
||It does boolean negation of the predicate on which it is invoked. Example:
Lets say we have the same code as used above for the filterList() method. I.e. filterList will filter the input Integers List based on the predicate passed to it.
What we are going to change is we will pass different predicates ANDed, ORed, Negated using the default methods and see the resultant output in the table below
(Note – I am assuming that all readers are aware of AND/ OR/ NOT operations in boolean logic)
|Predicate passed||Values in filteredList||Explanation|
|Lets say values in input IntegerList are – [-10, 0, 1, 20, 101, 200]|
||[20,101,200]||Conditions for i greater than 0 and i greater than 10 leaves us with 3 filtered values 20,100 & 200.|
||[1,20,101,200]||Either i is greater than 0 or i > 10 gives us 4 filtered values 1,20,101 & 200.|
||[-10,0]||Negation of i greater than zero implies i is less than or equal to 0. This gives us 2 values -10 & 0|
isEqual()– Predicate interface also has a static method
isEqual()which can be used to compare 2 instances of Predicate functional interface.
Its defined as –
static <T> Predicate<T> isEqual(Object targetRef)
It returns a predicate that tests if two arguments are equal according to
Summary – We looked at what is java.util.function.Predicate functional interface, what is the advantage of having this interface, we looked at the source including the abstract method, then we looked at the default methods, saw examples of their usages and finally learned about the static method present in the Predicate Interface.