On 07/14/2014 12:51 PM, Paul Sandoz wrote:
On Jul 12, 2014, at 5:41 PM, Remi Forax <fo...@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.

it seems to be a limitation of flatMap in that case, no ?

RĂ©mi


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.

Reply via email to