How to Use Predicate in Java: Simple Guide with Examples
In Java,
Predicate is a functional interface used to test a condition on an object and return a boolean result. You use it by implementing its test() method, often with a lambda expression, to check if an object meets a condition.Syntax
The Predicate<T> interface has one abstract method test(T t) that returns true or false based on the condition you define.
You can create a Predicate using a lambda expression or method reference.
- T: the type of the input to the predicate.
- test(T t): evaluates the condition on the input and returns a boolean.
java
Predicate<String> isNotEmpty = s -> !s.isEmpty(); boolean result = isNotEmpty.test("Hello");
Example
This example shows how to use Predicate to check if a number is positive and filter a list of numbers using that condition.
java
import java.util.function.Predicate; import java.util.List; import java.util.ArrayList; public class PredicateExample { public static void main(String[] args) { Predicate<Integer> isPositive = n -> n > 0; System.out.println("Test 5 is positive: " + isPositive.test(5)); System.out.println("Test -3 is positive: " + isPositive.test(-3)); List<Integer> numbers = List.of(-2, 0, 3, 7, -5); List<Integer> positiveNumbers = new ArrayList<>(); for (Integer num : numbers) { if (isPositive.test(num)) { positiveNumbers.add(num); } } System.out.println("Positive numbers: " + positiveNumbers); } }
Output
Test 5 is positive: true
Test -3 is positive: false
Positive numbers: [3, 7]
Common Pitfalls
Common mistakes when using Predicate include:
- Not returning a boolean value from the
test()method. - Using side effects inside the predicate, which should be avoided because predicates are meant for testing conditions only.
- Confusing
Predicatewith other functional interfaces likeFunctionorConsumer.
Here is an example of a wrong and right way:
java
import java.util.function.Predicate; public class PredicatePitfall { public static void main(String[] args) { // Wrong: Predicate should return boolean, not void // Predicate<String> wrongPredicate = s -> System.out.println(s.isEmpty()); // Compilation error // Right: Return boolean result Predicate<String> rightPredicate = s -> s.isEmpty(); System.out.println("Is empty: " + rightPredicate.test("")); } }
Output
Is empty: true
Quick Reference
| Concept | Description | Example |
|---|---|---|
| Predicate | Functional interface for testing a condition on T | Predicate |
| test(T t) | Method to evaluate the condition and return boolean | isEven.test(4) // returns true |
| and() | Combines two predicates with logical AND | p1.and(p2) |
| or() | Combines two predicates with logical OR | p1.or(p2) |
| negate() | Negates the predicate condition | p.negate() |
Key Takeaways
Predicate tests a condition on an object and returns a boolean.
Use lambda expressions to create concise Predicate implementations.
Avoid side effects inside Predicate; it should only test conditions.
Combine predicates with and(), or(), and negate() for complex logic.
Always return a boolean from the test() method.