Java 8 - Predicate example

Predicate is a functional interface that takes a Type T argument and returns a boolean.

public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);

    ....
}

Example 1 : test()

Passing an argument to test() returns a boolean.

import java.util.function.Predicate;

public class PredicateExample1 {

    public static void main(String[] args) {

        Predicate<Integer> predicate = (num) -> num > 10;

        boolean result = predicate.test(100);

        System.out.println(result);
    }
}

Output:

true

Example 2 : and()

and() is a method that chains Predicates. Performs AND operation on the results of Predicates and returns the result.

In the example below, predicate1.and(predicate2).test(25) returns true when both predicate1 and predicate2 are true. If either of them is false, false is returned.

import java.util.function.Predicate;

public class PredicateExample2 {

    public static void main(String[] args) {

        Predicate<Integer> predicate1 = (num) -> num > 10;
        Predicate<Integer> predicate2 = (num) -> num < 20;

        boolean result = predicate1.and(predicate2).test(25);
        System.out.println("25 : 10 < num < 20 ? " + result);

        result = predicate1.and(predicate2).test(15);
        System.out.println("15: 10 < num < 20 ? " + result);
    }
}

Output:

25 : 10 < num < 20 ? false
15: 10 < num < 20 ? true

Example 3 : or()

or() is a method that connects Predicates like and(). However, or() returns the result of the OR operation of Predicates.

In the example below, predicate1.or(predicate2).test(5) returns True if any one of predicate1 and predicate2 becomes True.

import java.util.function.Predicate;

public class PredicateExample5 {

    public static void main(String[] args) {

        Predicate<Integer> predicate1 = (num) -> num > 10;
        Predicate<Integer> predicate2 = (num) -> num < 0;

        boolean result = predicate1.or(predicate2).test(5);
        System.out.println("5 : num < 0 or num > 10 ? " + result);

        result = predicate1.or(predicate2).test(15);
        System.out.println("15 : num < 0 or num > 10 ? " + result);

        result = predicate1.or(predicate2).test(-5);
        System.out.println("-5 : num < 0 or num > 10 ? " + result);
    }
}

Output:

5 : num < 0 or num > 10 ? false
15 : num < 0 or num > 10 ? true
-5 : num < 0 or num > 10 ? true

Example 4 : isEqual()

isEqual() can also be used on Streams, it checks if it is equal to the object passed as an argument.

import java.util.function.Predicate;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class PredicateExample4 {

    public static void main(String[] args) {

        Stream<Integer> stream = IntStream.range(1, 10).boxed();

        stream.filter(Predicate.isEqual(5))
                .forEach(System.out::println);
    }
}

Output:

5

Example 5 : negate()

negate() returns a Predicate that returns the opposite of the value returned by the Predicate. You can think of the logical operator NOT as predicated before the predicate. It can be used to create logically opposing Predicates.

import java.util.function.Predicate;

public class PredicateExample6 {

    public static void main(String[] args) {

        Predicate<Integer> predicate = (num) -> num > 10;
        Predicate<Integer> negatePredicate = predicate.negate();

        boolean result = predicate.test(100);
        System.out.println("100 is greater than 10 ? " + result);

        result = negatePredicate.test(100);
        System.out.println("100 is less than 10 ? " + result);
    }
}

Output:

100 is greater than 10 ? true
100 is less than 10 ? false

Example 6 : Stream

Predicate can be passed as an argument to filter() of Stream.

import java.util.function.Predicate;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class PredicateExample3 {

    public static void main(String[] args) {

        Stream<Integer> stream = IntStream.range(1, 10).boxed();

        Predicate<Integer> predicate = num -> num > 5;

        stream.filter(predicate)
                .forEach(System.out::println);
    }
}

Output:

6
7
8
9

References

codechachaCopyright ©2019 codechacha