Skip to content
Advertisement

Java 8 streams when and why [closed]

I recently started working on a project where they encourage writing code with streams lambdas and such. Basically, the functional programming approach. While I find streams appealing, I have a few doubts about them. They are as follows.

  1. Performance – are serial streams really faster and more scalable than corresponding collections? Or are streams only preferable because someday we might use the stream().parallel() version?

  2. Memory usage – are streams a burden on the heap memory given that the terminal operations like collect(toList()) usually create a new object?

  3. Garbage collection (GC) – are streams more GC friendly than collections?

  4. Programming paradigm – I personally think mixing the functional programming style with OOPs is kind of gonna result in issues.

  5. Debugging – I personally debug my code by pen and paper rather than using a debugger (which some people might prefer). How good are streams when it comes to debugging?

  6. Operations Complexity – when it comes to writing everyday code (filtering grouping collecting mapping) streams are a cakewalk, but I find that when I have to write complex logic I end up resorting to the old collection based approach as it is more tweakable. Am I the only one doing this?

I understand that I am asking multiple questions here, but really they are 6 parts of the same question mentioned in the title. Hope for at least a summary like answer to each of these sub-questions. It’d be helpful if someone also could add a link to dive deep into all of these.

cheers!!

Advertisement

Answer

Performance – are serial streams really faster and more scalable than corresponding collections?

No. At least, not on average … with current Stream implementations.

Or are streams only preferable because someday we might use the stream().parallel() version?

Possibly yes. However, for many use-cases, the overheads of using parallel() outweigh the possible speedup.

Memory usage – are streams a burden on the heap memory given that the terminal operations like collect(toList()) usually create a new object?

AFAIK, No. There is typically no reduction in memory usage.

Garbage collection (GC) – are streams more GC friendly than collections?

AFAIK, No.

Programming paradigm – I personally think mixing the functional programming style with OOPs is kind of gonna result in issues.

That is your opinion.

If you stick with making your stream operations side-effect free, there shouldn’t be any issues.

  • The documentation recommends against side-effects in stream operations.
  • If you rely on side-effects, that’s not functional.

Debugging – I personally debug my code by pen and paper rather than using a debugger (which some people might prefer). How good are streams when it comes to debugging?

That’s a matter of opinion. I personally think that it makes no difference to debugging.

Operations Complexity – when it comes to writing everyday code (filtering grouping collecting mapping) streams are a cakewalk, but I find that when I have to write complex logic I end up resorting to the old collection based approach as it is more tweakable. Am I the only one doing this?

You are not the only one. On the other a lot of people do a lot more complicated things using than simple filtering, grouping, collecting and mapping. The more you use streams, the better you will get at spotting other use-cases. But the flip-side is that some people seem to want to do things with streams that they probably shouldn’t.


I recently started working on a project where they encourage writing code with streams lambdas and such.

That’s between you and the rest of the team. I don’t think it is my / our business to get into your project team’s debates on this.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement