Java Predicate Interface

In Java, the Predicate interface is a functional interface that represents a boolean-valued function of one argument. It is part of the java.util.function package and is used to test the values of objects and determine whether they meet certain criteria.

Here is the definition of the Predicate interface:

@FunctionalInterface
public interface Predicate<T> {
  boolean test(T t);
}

The Predicate interface has a single abstract method, test(), which takes an argument of type T and returns a boolean value. The T type is a type parameter that specifies the type of the argument being tested.

Here is an example of how to use the Predicate interface:

Predicate<Integer> isPositive = x -> x > 0;

System.out.println(isPositive.test(10));  // prints true
System.out.println(isPositive.test(-5));  // prints false

In this example, the isPositive predicate tests whether an integer value is positive (greater than 0). The test() method is called with different integer values, and it returns true or false depending on whether the value is positive or not.

The Predicate interface provides several default methods that can be used to combine multiple predicates or transform the values being tested. For example, the and() method can be used to combine two predicates using a logical AND, while the negate() method can be used to negate the result of a predicate.

Here is an example of using the and() and negate() methods of the Predicate interface:

Predicate<String> isEmpty = String::isEmpty;
Predicate<String> isNotEmpty = isEmpty.negate();
Predicate<String> isNotEmptyAndStartsWithA = isNotEmpty.and(s -> s.startsWith("A"));

System.out.println(isEmpty.test(""));  // prints true
System.out.println(isEmpty.test("Hello"));  // prints false
System.out.println(isNotEmpty.test("Hello"));  // prints true
System.out.println(isNotEmptyAndStartsWithA.test("Hello"));  // prints false
System.out.println(isNotEmptyAndStartsWithA.test("Apple"));  // prints true

In this example, we define three predicates:

  • isEmpty tests whether a string is empty. It uses the isEmpty() method of the String class as its implementation.
  • isNotEmpty is the negation of isEmpty. It returns true for non-empty strings and false for empty strings.
  • isNotEmptyAndStartsWithA is the conjunction of isNotEmpty and a predicate that tests whether a string starts with the letter 'A'. It returns true only for non-empty strings that start with 'A', and false for all other strings.

We then use the test() method of each predicate to test various strings and see whether they meet the criteria specified by the predicate.

Here is an example of using the or() method of the Predicate interface to combine two predicates using a logical OR:

Predicate<String> isEmpty = String::isEmpty;
Predicate<String> isLongerThan10 = s -> s.length() > 10;
Predicate<String> isEmptyOrLongerThan10 = isEmpty.or(isLongerThan10);

System.out.println(isEmptyOrLongerThan10.test(""));  // prints true
System.out.println(isEmptyOrLongerThan10.test("Hello"));  // prints false
System.out.println(isEmptyOrLongerThan10.test("This string is longer than 10 characters"));  // prints true

In this example, we define three predicates:

  • isEmpty tests whether a string is empty. It uses the isEmpty() method of the String class as its implementation.
  • isLongerThan10 tests whether a string is longer than 10 characters.
  • isEmptyOrLongerThan10 is the logical OR of isEmpty and isLongerThan10. It returns true for empty strings and strings that are longer than 10 characters, and false for all other strings.

We then use the test() method of the isEmptyOrLongerThan10 predicate to test various strings and see whether they meet the criteria specified by the predicate.

Here is an example of using the isEqual() method of the Predicate interface to test if an object is equal to a specified object:

Predicate<String> isFalse = Predicate.isEqual("false");

System.out.println(isFalse.test("false"));  // prints true
System.out.println(isFalse.test("true"));  // prints false
System.out.println(isFalse.test("False"));  // prints false

In this example, we define a predicate, isFalse, that tests if an object is equal to the string "false". We then use the test() method of the isFalse predicate to test various strings and see whether they are equal to the string "false".

It's worth noting that the isEqual() method uses the equals() method to compare objects, so it takes into account the type of the objects being compared as well as their values. This means that the isFalse predicate will return false for objects of a different type, even if they have the same value.