
Predicate is a Functional Interface (an Interface with only one abstract method) that was introduced in Java 8 in the java.util.function package.
| Method | Description | Return Type | Exceptions | Input Type | Added in JDK |
|---|---|---|---|---|---|
test(T t) | Evaluates this predicate on the given argument. | boolean | T | Java 8 | |
and(Predicate<? super T> other) | Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another. | Predicate<T> | NullPointerException if other is null | T | Java 8 |
negate() | Returns a predicate that represents the logical negation of this predicate. | Predicate<T> | N/A | Java 8 | |
or(Predicate<? super T> other) | Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another. | Predicate<T> | NullPointerException if other is null | T | Java 8 |
isEqual(Object targetRef) | Returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object). | Predicate<T> | T | Java 8 | |
not(Predicate<? super T> target) | Returns a predicate that is the negation of the supplied predicate. | Predicate<T> | NullPointerException if target is null | T | Java 11 |
The test() is the abstract method that returns a boolean by predicting whether the performed condition is true or not for the provided generic type argument T.
Example:
public static void main(String[] args) {
Predicate<Integer> evenOrOdd = num -> num % 2 == 0;
int testNumberForEvenOdd = 12;
boolean isEven = evenOrOdd.test(testNumberForEvenOdd);
System.out.println(testNumberForEvenOdd + " is Even: " + isEven);
}
Output:
Official Java 8 Predicate Documentation:
test(T t): Evaluates this predicate on the given argument.and(Predicate<? super T> other): Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.negate(): Returns a predicate that represents the logical negation of this predicate.or(Predicate<? super T> other): Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.isEqual(Object targetRef): Returns a predicate that tests if two arguments are equal according toObjects.equals(Object, Object).not(Predicate<? super T> target): Returns a predicate that is the negation of the supplied predicate.
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!