Declarative iteration describes the transformation or processing a sequence should produce instead of spelling out loop control flow. Flat pipeline operations can reduce nesting and cognitive load, while keeping each step named by its intent. section 3.2.3
Email reminders with a stream pipeline
An explicit loop checks each customer before sending an email. A stream expresses the same story as filtering eligible customers, then sending each reminder: customers.stream().filter(this::shouldSendReminder).forEach(customer -> sendEmail(customer, promotionCode)).
Use declarative iteration when it improves the code’s readable story:
- Prefer flat pipeline operations when they simplify control flow and remove unnecessary nesting. section 3.2.3
- Keep a straightforward loop when a stream pipeline would be harder to understand; lower metrics do not guarantee clearer code. section 3.2.3
