What is the purpose of the Predicate functional interface in Java 8?

Predicate<T> is a functional interface in java.util.function representing a boolean-valued function of one argument, defined by its single abstract method test(T t). It's used wherever code needs to evaluate a true/false condition against an object.

Key Points: • Predicate's core method is boolean test(T t), making any lambda or method reference of that shape usable as a Predicate. • It's the parameter type expected by Stream.filter(), Collection.removeIf(), and similar conditional APIs. • Predicate provides default methods and(), or(), and negate() to combine multiple predicates without writing custom boolean logic. • There are primitive specializations like IntPredicate to avoid boxing overhead when testing primitive values. • Predicates are stateless by convention — they should not have side effects, since the runtime may evaluate them in any order, especially in parallel streams.

Example: Predicate<String> isLong = s -> s.length() > 5; combined with another predicate via isLong.and(s -> s.startsWith("A")) filters strings that are both long and start with "A".

Code Example:

Predicate<Integer> isEven = n -> n % 2 == 0;
Predicate<Integer> isPositive = n -> n > 0;

List<Integer> nums = Arrays.asList(-4, 3, 6, -2, 8);

List<Integer> result = nums.stream()
        .filter(isEven.and(isPositive))
        .collect(Collectors.toList());

Interview Tip: A concise interview answer is:

"Predicate represents a boolean-valued test on a single argument through its test() method, and it's what filter() and removeIf() expect. Its real value is the default combinator methods — and(), or(), and negate() — which let me compose multiple conditions cleanly instead of writing one big lambda with embedded boolean logic."