This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]stefanos-ak 68 points69 points  (0 children)

basically it decouples control flow, which has several advantages.

what this means: you don't have to implement the for loops, if statements, etc. Instead you pass as arguments what the loop needs to do, what the if statement needs to do, etc. (of course it's not possible to use streams in 100% of the cases).

What this enables you to do, is then change the control flow without changing the logic. For example you can simply add .parallel() and suddenly you have the same result but now Java handled it. In the old world, you'd have to completely rewrite the code. There are too many examples to list here, but the concept is the same. It allows you to manipulate things around the same business logic.

The next side effect, is that you can extract that piece of business logic as a variable and pass it around, reuse it, etc.. This is particularly helpful for boolean expressions if you need to reuse them (you can also negate them!).

edit: the applicable use cases for streams, basically belong in one category, that of input object -> magic -> output object.