JB Header
Java 8 java.util.function.Predicate tutorial with examples
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. java.util.function.Predicate source
java.util.function.Predicate source code
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface Predicate<T> {
 boolean test(T t);
//rest of the code goes here
}
Explanation of the code
  • 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 Predicate.
  • 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
Code showing Predicate being used for a boolean condition check
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class PredicateFunctionExample{
 public static void main(String args[]){
  Predicate<Integer> positive = i -> i > 0;
  List<Integer> integerList = Arrays.asList(
                      new Integer(1),new Integer(10),
                      new Integer(200),new Integer(101), 
                      new Integer(-10),new Integer(0));
  List<Integer> filteredList = filterList(integerList, positive);
  filteredList.forEach(System.out::println);
 }
 public static List<Integer> filterList(List<Integer> listOfIntegers, Predicate<Integer> predicate){
  List<Integer> filteredList = new ArrayList<Integer>();
  for(Integer integer:listOfIntegers){
   if(predicate.test(integer)){
    filteredList.add(integer);
   }
  }
  return filteredList;
 }
}
 OUTPUT of the above code
1  10  200  101
Explanation of the code and output
  • 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.
  • The filterList() method loops through the whole IntegerList and whichever integer passes the condition test it is added to the resultant list called filteredList.
  • 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 > 0 assigned to an instance of type Predicate named positive.
  • This predicate instance positive is then passed as the second argument of filterList() method
  • The input Integer list 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 Predicate interface
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
and() It does logical AND of the predicate on which it is called with another predicate. Example: predicate1.and(predicate2)
or() It does logical OR of the predicate on which it is called with another predicate. Example: predicate1.or(predicate2)
negate() It does boolean negation of the predicate on which it is invoked. Example: predicate1.negate()
Where, predicate1 & predicate2 are instances of Predicate interface/lambda expression/method references
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]
(i->i>0).and(i->i>10) [20,101,200] Conditions for i greater than 0 and i greater than 10 leaves us with 3 filtered values 20,100 & 200.
(i->i>0).or(i->i>10) [1,20,101,200] Either i is greater than 0 or i > 10 gives us 4 filtered values 1,20,101 & 200.
(i->i>0).negate() [-10,0] Negation of i greater than zero implies i is less than or equal to 0. This gives us 2 values -10 & 0
Static method 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 Objects.equals(Object, Object).
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.