Java allows you to process data in collections or streams. Itโs very easy to think of streams as a technique to turn one collection into another. This can lead to some rather casual code where streaming data is repeatedly collected to some sort of collection, passed as a whole collection, and then processed some more.
For 6 elements, who cares!
Example:
01 02 03 04 05 06 07 08 09 10 11 | // imaginary inputList<String> list = asList("Foo", "Bar", "Baz");// conversion and sendingList<String> bs = list.stream() .filter(item -> item.startsWith("B")) .collect(toList());List<Wrapped> wrapped = bs.stream() .map(Wrapped::new) .collect(toList());sendWrappedItems(wrapped.stream()); |
The above suffers from a code-smell, which is the constant collection and restreaming of a stream, and most people would probably notice that and remove some of the interim lists if it was all one method.
Most people would. Iโve seen people not do this.
However, if the above were using subroutines to process things, itโs quite easy to optimise the simplicity of the subroutinesโ APIs and make them receive and return a collection. This was you can end up with the above behaviour.
The solution is to look at the pipeline of data processing at the high level in terms of filter, map, and reduce type functions and try to model it around streams.
But Why?
Treat Streams as Though Theyโre Infinite
We have small containers these days and we want them to get the most of their resources. A small container, if running continuously can process an unbounded stream of data. If we imagine that all our data is a potentially infinite stream, and design our software to use streaming to avoid getting all of it into memory, then two good things happen:
- We optimise the max memory requirement of our streams to be as low as possible for ALL cases
- We HAVE to use the Streaming API properly and we end up with cleaner code, as the declarative aspect of the Stream API helps describe whatโs happening in the data conversion. We probably even lost some horribly named temporary variables in the processโฆ
The above code then becomes:
1 2 3 4 5 6 7 | // imaginary inputList<String> list = asList("Foo", "Bar", "Baz");// conversion and sendingsendWrappedItems(list.stream() .filter(item -> item.startsWith("B")) .map(Wrapped::new)); |
Published on Java Code Geeks with permission by Ashley Frieze, partner at our JCG program. See the original article here: To Infinity (Streams) and Beyond! Opinions expressed by Java Code Geeks contributors are their own. |
Thank you!
We will contact you soon.
Ashley FriezeNovember 22nd, 2019Last Updated: November 21st, 2019

This site uses Akismet to reduce spam. Learn how your comment data is processed.