Stream.limit() - puzzler/bug/feature
Paul Sandoz
paul.sandoz at oracle.com
Thu Nov 15 02:31:44 PST 2012
Hi,
On Nov 15, 2012, at 9:32 AM, Dmitry Bessonov <dmitry.bessonov at oracle.com> wrote:
> Hello,
>
> While playing with method Sream.limit(int) using a mini-code like
>
> final Stream<Integer> s = Arrays.asStream(1, 2, 3, 4, 5);
> final Stream<Integer> to3 = s.limit(3);
> final Stream<Integer> to4 = s.limit(4);
>
> 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.
>
Right.
> Might situations (and mis-usage?) like this above
> be a trap for novice users
> that should be well described?
>
The limit is is really "at most", so the stream can contain less than the limit but no more than. I think we just need to describe it in terms of that.
Stream.iterator() is like a "get of jail" card for those that need to do something on streams that cannot be done with other terminal operations. So IMHO i don't think we need to go to great lengths to describe such potential traps.
Paul.
More information about the lambda-dev
mailing list