No-reuse-streams
Paul Sandoz
paul.sandoz at oracle.com
Mon Nov 26 09:22:52 PST 2012
On Nov 26, 2012, at 5:45 PM, Henry Jen <henry.jen at oracle.com> wrote:
>
> On Nov 26, 2012, at 8:21 AM, "David M. Lloyd" <david.lloyd at redhat.com> wrote:
>
>>
>>> Should the following throw an ISE on the last line of the following?
>>>
>>> Stream s = ..
>>> Object i1 = s.findFirst();
>>> Stream s1 = s.map(...);
>>>
>>> i.e. should we fail on the s.map(...) or just when a terminal operation occurs, if at all?
>>
>> I agree with you and Rémi - this should fail.
>>
>
>
>
> While it may be "consistent", what is the obvious alternative when need to do something like this?
> A use case like this is probably following,
>
> For a input stream, find a marker, and then continue to process the rest of stream.
>
That seems like a use-case for dropWhile or skipWhile.
> I guess the general question is: how does one control the flow of the stream? How do we continue the stream after a terminal op which does not consume whole stream.
>
It's a bit funky but one could do:
Stream s = ...
Iterator i = s.iterator();
Object first = i.next();
i.stream()....; // Not yet supported but IIRC we have talked about making Iterator streamable.
If the source is an iterator of some sorts i think detached streams might be useful for repeated processing of such a source.
Note that for a parallel stream findFirst and findAny (or anyMatch) may consume more elements than the equivalent sequential stream.
Paul.
More information about the lambda-libs-spec-observers
mailing list