Predicate Interface

Description

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.

Example

The following example shows how to use Predicate.


import java.util.function.Predicate;
//from   w ww  .  jav a 2  s .co  m
public class Main {

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

The code above generates the following result.





















Home »
  Java Lambda »
    Java Lambda Tutorial »




Java Lambda Tutorial