Jonathan
1 min readJun 9, 2021

--

Nice article. When working with primitives, it's recommended to use IntStream, LongStream or DoubleStream, rather than regular Stream class. Boxing and unboxing primitive values are very expensive, and it happens when you operate (sum, mul, div) a boxed value.

In my opinion, the biggest advantage of Streams is that, intermediate operations, such as filter and map, does not create intermediate collections, so, when working with infinite streams or big amount of data, you will not waste memory and CPU time with allocation of those auxiliar collections. Achieving the same without Lazy-evaluated Streams is not easy and readable as much as Streams (unless escape analysis take in place and optimize those allocations). Also, Streams could be parallelized and used to receive data from database on-demand.

On performance side, GraalVM EE runs Stream very well, compared to other JVM implementations. Streams have their purpose, we always need to evaluate our needs before chosing an approach over the other one. Plain for loops does have better performance, but they are mostly stack allocations with lower indirection; on the other side, Streams are complex structures with more indirection and more heap allocations (even with all optimizations applied), so they have worse performance for simple scenarios, in exchange for being more readable and scalable for complex scenarios with bigger amount of data.

--

--

Jonathan
Jonathan

Written by Jonathan

Developer, Bytecode Engineer, Cyber security analyst, Workaholic, Critic, Writer.

No responses yet