Java 8 Streams provide the filter() method to process and filter data based on specific conditions.
In most cases, developers apply a single condition using filter(). However, real-world applications often require filtering data using multiple conditions.
There are multiple ways to achieve this:
- Calling
filter()multiple times - Combining predicates using
Predicate.and() - Combining predicates using
Predicate.or() - Using
Predicate.negate() - Using
Predicate.isEqual()
This guide demonstrates all these approaches with examples.
2. Stream.filter() with a Single Condition
The filter() method accepts a Predicate<T>.
A predicate is a functional interface that evaluates a condition and returns:
true→ element is retainedfalse→ element is removed
Example
Output
Fruits stream : [mango, grapes, apple, papaya, jack fruit, dragon fruit]
filteredList : [mango, grapes, apple, papaya]
noLetterOFilterList : [grapes, apple, papaya, jack fruit]
Explanation
Filter 1
Removes:
- jack fruit
- dragon fruit
Result:
[mango, grapes, apple, papaya]
Filter 2
Removes:
- mango
- dragon fruit
Result:
[grapes, apple, papaya, jack fruit]
3. Stream.filter() with Multiple Conditions
There are two common approaches:
- Apply multiple
filter()calls - Combine predicates using
Predicate.and()
3.1 Calling filter() Multiple Times
Each filter() acts as an additional condition.
Example
Output
Final result : [grapes, apple, papaya]
Execution Flow
Original Data
[mango, grapes, apple, papaya, jack fruit, dragon fruit]
After First Filter
Removes:
jack fruit
dragon fruit
Result:
[mango, grapes, apple, papaya]
After Second Filter
Removes:
mango
Result:
[grapes, apple, papaya]
3.2 Using Predicate.and()
Instead of multiple filter() calls, predicates can be combined into a single predicate.
Example
Output
[grapes, apple, papaya]
Explanation
is equivalent to:
Both predicates must return true.
Internally:
4. Using Multiple Predicates with and()
You can chain as many predicates as required.
Example
Equivalent to:
5. Using Predicate.or()
Use or() when at least one condition should be true.
Example
Output
[mango, jack fruit, dragon fruit]
Equivalent Logic
6. Using Predicate.negate()
negate() reverses a predicate.
Example
Equivalent
Output
[mango, grapes, apple, papaya]
7. Real-World Example with Employee Objects
Employee Class
Filtering Employees
Output
[David, Mike]
Combined Predicate Version
8. Performance Considerations
Multiple filter() Calls
Combined Predicate
Both are lazy operations and generally provide similar performance.
Choose based on readability:
- Use multiple
filter()calls for readability. - Use
Predicate.and()when predicates are reusable.
Example:
This is often easier to read than:
9. Summary
| Approach | Description |
|---|---|
filter(predicate) | Single condition filtering |
filter().filter() | Multiple conditions using separate filters |
Predicate.and() | All conditions must be true |
Predicate.or() | At least one condition must be true |
Predicate.negate() | Reverse a condition |
Predicate.isEqual() | Equality comparison |
Key Takeaways
filter()accepts aPredicate.- Multiple conditions can be applied by chaining
filter()methods. Predicate.and()combines conditions using logical AND (&&).Predicate.or()combines conditions using logical OR (||).Predicate.negate()reverses the result of a predicate.- Both chained filters and combined predicates produce the same logical result.
- Choose the approach that improves readability and maintainability.
Post a Comment