Java Lambda - Java Predicate Interface

Predicate interface contains the following default and static methods.

We can use NOT, AND, and OR methods to create a predicate based on other predicates.

default  Predicate<T> negate()
default  Predicate<T> and(Predicate<? super T> other)
default  Predicate<T> or(Predicate<?  super T> other)
static <T> Predicate<T> isEqual(Object  targetRef)

negate() negates a Predicate from the original predicate.

and() combines two predicates with a short-circuiting logical AND.

or() combines two predicates with a short-circuiting logical OR.

isEqual() returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).

We can chain the above methods to create complex predicates.


The following example shows how to use Predicate.

import java.util.function.Predicate;
//from w w w  .j  a  v  a2 s.  c om
public class Main {

  public static void main(String[] args) {
    Predicate<String> i  = (s)-> s.length() > 5;
    System.out.println(i.test(" "));

The code above generates the following result.