Predicate

Class form of a predicate

A predicate can be written in class form as seen below.

  public class EvenPredicate implements Predicate<Integer> {

    public boolean test(Integer value) {
      return value.intValue() % 2 == 0;
    }
  }

The example above defines the class EvenPredicate and it can be used to test whether an integer is odd or even.

Lambda expression form of a predicate

A predicate can be written in lambda expression form as illustrated below.

  Predicate<Integer> even = e -> e % 2 == 0;

The example above creates a predicate that could be used to test whether an integer is odd or even.

Using a predicate for testing

A predicate can be used for testing using its test method as shown below.

  Predicate<Integer> even = e -> e % 2 == 0;
  even.test(1);

The example above illustrates how the predicate can be used to test if a given integer is even. If the integer is even it would return true, if the integer is not even it would return false.

Negating a predicate

The Predicate class has a static method that wraps an existing predicate and negates the boolean result. See for an example below.

  Predicate<Integer> even = e -> e % 2 == 0;
  Predicate<Integer> notEven = Predicate.not(even);
  notEven.test(1);

The example above illustrates how to negate the even predicate making it the notEven predicate as illustrated.

Testing equality using a predicate

The Predicate class has a static method that returns a predicate that will perform an .equals check against the given object.

  Predicate<Integer> isZero = Predicate.isEqual(0);
  isZero.test(0);

The example above creates a predicate that can be used to determine if a given number is equals to 0.

Combining predicates using a logical AND

The Predicate class has a static method that returns a predicate that combines the result of 2 predicates by means of a logical AND.

  Predicate<Integer> isNotZero = e -> e != 0;
  Predicate<Integer> even = e -> e % 2 == 0;
  Predicate<Integer> isNotZeroAndEven = isNotZero.and(even);
  isNotZeroAndEven.test(2);

The example above combines the isNotZero predicate and the even predicate together to construct the isNotZeroAndEven predicate.

Combining predicates using a logical OR

The Predicate class has a static method that returns a predicate that combines the result of 2 predicates by means of a logical OR.

  Predicate<Integer> isZero = e -> e == 0;
  Predicate<Integer> even = e -> e % 2 == 0;
  Predicate<Integer> isZeroOrEven = isZero.or(even);
  isZeroOrEven.test(2);

The example above combines the isZero predicate and the even predicate together to construct the isZeroOrEven predicate.

References

  1. JavaDoc

results matching ""

    No results matching ""