Stream.concat
Paul Sandoz
paul.sandoz at oracle.com
Mon Jul 14 10:51:26 UTC 2014
On Jul 12, 2014, at 5:41 PM, Remi Forax <forax at univ-mlv.fr> wrote:
> I was not able to find the answer to my question in the archive,
> why Stream.concat is not implemented like this ?
>
> @SafeVarargs
> public static <T> Stream<T> concat(Stream<T>... streams) {
> return Arrays.stream(streams).flatMap(Function.identity());
> }
>
Because the capabilities and characteristics of the streams are then lost e.g. in this case the splitting is governed by the number of streams passed in.
The current implementation retains the splitting capabilities and merges characteristics from both streams. We kept it simpler for now with just two streams, but it should be possible to extend support with some additional complexity to say:
concat(Stream<T> a, Stream<T> b, Stream<? extends T>... rest)
A really simple layered implementation would be:
return Stream.concat(
Stream.concat(a, b),
Stream.of(rest).reduce(Stream.empty(), Stream::concat));
If too many streams are concatenated then the stack can blow up when operating on the result. See:
https://bugs.openjdk.java.net/browse/JDK-8025523
Paul.
More information about the lambda-dev
mailing list