Combine collection predicates (ARCH4077)
هذا المحتوى غير متوفر بلغتك بعد.
Key java.performance.combine-collection-predicates · Severity info · Category performance
Consecutive filter calls, or count() > 0 for an emptiness check, traverse the stream more than needed. They can be fused into a single predicate or anyMatch.
Incorrect
Section titled “Incorrect”boolean any = items.stream() .filter(Item::isActive) .filter(Item::isPaid) .count() > 0;Correct
Section titled “Correct”boolean any = items.stream() .anyMatch(i -> i.isActive() && i.isPaid());Fuse consecutive filters into one predicate and use anyMatch for presence checks.