Suppose you need to keep words longer than three characters and convert them to uppercase. A loop makes you spell out iteration, branching, and where to put each result. A declarative pipeline puts the transformation first: filter, map, then collect. section 6.1.2
Make the transformation visible
List<String> result = words.stream() .filter(w -> w.length() > 3) .map(String::toUpperCase) .collect(Collectors.toList()); section 6.1.2
This style works well when behavior can be composed from small functions. Pure functions depend only on their inputs and produce the same output for the same inputs, making each step easier to understand and test without tracking hidden state. section 6.1.2
Do not force every task into a pipeline. Writing files, updating databases, sending network messages, or relying on heavily mutated state may call for imperative code. An API may still make a pipeline clearer, but any side effects should remain explicit. For small, simple operations, imperative code can also avoid pipeline setup overhead. Choose the form that makes the work clear without hiding what it costs or changes. section 6.4













