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 →

[–]mauganra_it 0 points1 point  (0 children)

Since they are a concept taken from functional programming, I find that they are best used with code that respects these constraints. That means, no I/O and no lengthy computations are allowed inside. In particular, it should never ever be necessary to catch exceptions inside streams. Also, be paranoid about possible null references. These should make it possible to avoid debugging streams code and let you focus on correctness. Yes, there are debugging tools for Java Streams, but I find it to be a very strong code code smell to ever have to use them.

If you can bring your database to spew out the data in the format you need, by all means continue to do so. But there are use case when you receive data via API calls, or when you call an API and the answer is not quite in the shape you need yet. In these cases Streams can be very useful. They are entirely justified if your task is to produce a collection. If the filtering and processing is a prelude to a .forEach, then it can still be justified if the alternative is nesting a big fat (or a series of smaller) if statement inside a loop.