Java Predicate Interface

Introduction

In this blog we will learn how to use Predicate functional interface in java.util.Function.

Predicate means whether a given statement is true or false.

You can use predicate to test for conditions in your code.

For example, you may want to validate numbers,string or objects based on certain conditions.

Predicate<T> accepts the argument and returns boolean by performing business logic on the argument.

Predicate<T> functional method is boolean test(T t);.

In the below example we can see Predicate interface accepts a string as an argument and check if the length is greater than 10

Predicate example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import java.util.function.Predicate;

public class Examples {

    public static void main(String[] args) {

       Predicate<String> testLength= x -> x.length() < 10;

        System.out.println(testLength.test("Harish"));  // true
        System.out.println(testLength.test("This is a very long word")); //false
    }


}

Predicate example in filter method

In the below example we pass Predicate<Integer> to filter() method of stream to get only even number.

This is the most commonly use case for Predicate functional interface.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Examples {

    public static void main(String[] args) {

       Predicate<Integer> evenNumber= x -> x%2==0;

       List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9,10);

        List<Integer> result = nums.stream()
                .filter(evenNumber)
                .collect(Collectors.toList());

        System.out.println(result); //[2, 4, 6, 8, 10]

    }


}

Predicate chaining with and() method

If we have multiple predicates to validate, we can chain them together to validate complex business logic

In the below example we are chaining multiple conditions and ensuring all Predicates are true using and method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Examples {

    public static void main(String[] args) {

       Predicate<Integer> numgreaterthan5= x -> x>5;
        Predicate<Integer> lessthan20= x -> x<20;

       List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9,22,50);

        List<Integer> result = nums.stream()
                .filter(numgreaterthan5.and(lessthan20))
                .collect(Collectors.toList());
        System.out.println(result);  // [6, 7, 8, 9]
    }


}

Predicate chaining with or() method

In the below example we are chaining multiple conditions and ensuring atleast 1 Predicate is true using or method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Examples {

    public static void main(String[] args) {

       Predicate<String> startwithA= x -> x.startsWith("A");
        Predicate<String> containsrish= x -> x.contains("rish");

       List<String> names = Arrays.asList("Ashu","Tim","David","Harish","Mary");

        List<String> result = names.stream()
                .filter(startwithA.or(containsrish))
                .collect(Collectors.toList());

        System.out.println(result); // [Ashu, Harish]


    }


}

Predicate with negate

We can test negative boolean condition using negate method.

In the below example we test for all strings which donot start with ‘A’ using negate() method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Examples {

    public static void main(String[] args) {

       Predicate<String> startwithA= x -> x.startsWith("A");

       List<String> names = Arrays.asList("Ashu","Tim","David","Harish","Mary");

        List<String> result = names.stream()
                .filter(startwithA.negate())
                .collect(Collectors.toList());
        System.out.println(result); // [Tim, David, Harish, Mary]


    }


}

Predicate with isEqual method

Predicate with isEqual method is used to check if 2 objects are equal to each other.

As per documentation,

“Returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object)”

In the below example we check if string is equal as mentioned in Predicate.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import java.util.function.Predicate;

public class Examples {

    public static void main(String[] args) {

       Predicate<String> isEqualtest= Predicate.isEqual("Harish");
       System.out.println(isEqualtest.test("Tim"));    //false 
       System.out.println(isEqualtest.test("Harish")); //true


    }


}

Conclusion

I hope this tutorial has helped you to understand Predicate Interface in Java.

Learn more functional interfaces

  1. Function interface
  2. Consumer interface
  3. BiPredicate interface
  4. BiFunction interface
  5. Supplier interface