Stream.limit() - puzzler/bug/feature

Marcus Thiesen marcus at thiesen.org
Thu Nov 15 09:11:48 PST 2012


Hey List,

sorry, I haven't followed the whole Stream discussion and without publicliy
available JavaDocs (are there?) I'm currently trying to figure out what
this comment means, because without all that background I read the code as
annotated:

2012/11/15 Dmitry Bessonov <dmitry.bessonov at oracle.com>

> While playing with method Sream.limit(int) using a mini-code like
>
>          final Stream<Integer> s = Arrays.asStream(1, 2, 3, 4, 5);
>

Give me a "Stream" view of the Array int[] 1, 2, 3, 4, 5.



>          final Stream<Integer> to3 = s.limit(3);
>

Give me a Stream view of s limitted to 3 values.


>          final Stream<Integer> to4 = s.limit(4);
>

Give me a Stream view of s limitted to 4 values.


>
> have to admit that there's no unambiguous answer
> to the question of contents  of streams "to3" and "to4".
> It depends on which of the streams is consumed first.
>
>
My best guess would be to3 = [ 1, 2, 3 ] and to4 = [ 1, 2, 3, 4 ].

Given my naive understanding of the above code the comment does not make
sense. The only way the above comment makes sense is that we are not
talking of (somehow immutable) views here but that those operations mutate
 the backing array on read time of the resulting stream. Am I right?

Cheers,
  Marcus




-- 
Marcus Thiesen :: www.thiesen.org :: @mthiesen :: 0173 / 28 01 82 4


More information about the lambda-dev mailing list