The goal: to merge two collections into (a third) one
I like the fluency of this solution (it only takes one statement):
Stream.concat(collection.stream(), anotherCollection.stream())
.collect(/* some Collector */)
However, my latest PR commit, though accepted and merged, was edited to replace that line with this (notice there are now twice as many statements):
Collection</* some type */> mergedCollection = new /* Collection implementation */<>(collection);
mergedCollection.addAll(anotherCollection);
I don't want to bother the maintainer who made the change so I decided to ask the SO community:
Are there any drawbacks in merging collections with Stream.concat()?
Its main disadvantage to using
Stream#concatis it only accepts 2 streams. It does what it says: concatenates two streams together into one.If you need to concatenate three or more streams, use
Stream#of, however, it comes with flat mapping:Flat mapping itself is not bad but relies on a stream's existence for the concatenation. It is not ideal if there are a lot of nested structures as the result is an unnecessary number of streams created temporarily only for flat mapping and the performance might be hit with larger numbers.
What to do? The best option is
Stream#mapMultito avoid excessive creation of additional streams:To understand it better, here is the expanded notation using a lambda expression instead of a method reference:
This was invented as of Java 16 and I wrote another answer regarding its use.