Talking about Stream
s, when I execute this piece of code
public class Main {
public static void main(String[] args) {
Stream.of(1,2,3,4,5,6,7,8,9)
.peek(x->System.out.print("\nA"+x))
.limit(3)
.peek(x->System.out.print("B"+x))
.forEach(x->System.out.print("C"+x));
}
}
I get this output
A1B1C1
A2B2C2
A3B3C3
because limiting my stream to the first three components forces actions A, B and C to be executed only three times.
Trying to perform an analogous computation on the last three elements by using skip()
method, shows a different behaviour: this
public class Main {
public static void main(String[] args) {
Stream.of(1,2,3,4,5,6,7,8,9)
.peek(x->System.out.print("\nA"+x))
.skip(6)
.peek(x->System.out.print("B"+x))
.forEach(x->System.out.print("C"+x));
}
}
outputs this
A1
A2
A3
A4
A5
A6
A7B7C7
A8B8C8
A9B9C9
Why, in this case, actions A1 to A6 are being executed? It must have something to do with the fact that limit is a short-circuiting stateful intermediate operation, while skip is not, but I don't understand practical implications of this property. Is it just that "every action before skip is executed while not everyone before limit is"?
Maybe this little diagram helps to get some natural "feeling" for how the stream is processed.
The first line
=>8=>=7=
...===
depicts the stream. The elements 1..8 are flowing from the left to the right. There are three "windows":peek A
) you see everythingskip 6
orlimit 3
) a kind of filtering is done. Either the first or the last elements are "eliminated" - means not passed on for further processing.┌────────────────────────────────────────────────────────────────────────────┐ │ │ │▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸ ▸▸▸▸▸▸▸▸▸▸▸ ▸▸▸▸▸▸▸▸▸▸ ▸▸▸▸▸▸▸▸▸ │ │ 8 7 6 5 4 3 2 1 │ │▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸ ▲ ▸▸▸▸▸▸▸▸▸▸▸ ▲ ▸▸▸▸▸▸▸▸▸▸ ▲ ▸▸▸▸▸▸▸▸▸ │ │ │ │ │ │ │ │ skip 6 │ │ │ peek A limit 3 peek B │ └────────────────────────────────────────────────────────────────────────────┘
Probably not everything (maybe not even anything) in this explanation is technically completely correct. But when I see it like this it's quite clear to me what items reach which of the concatenated instructions.
The fluent notation of the streamed pipeline is what's causing this confusion. Think about it this way:
limit(3)
All the pipelined operations are evaluated lazily, except
forEach()
, which is a terminal operation, which triggers "execution of the pipeline".When the pipeline is executed, intermediary stream definitions will not make any assumptions about what happens "before" or "after". All they're doing is take an input stream and transform it into an output stream:
s1
contains 9 differentInteger
values.s2
peeks at all values that pass it and prints them.s3
passes the first 3 values tos4
and aborts the pipeline after the third value. No further values are produced bys3
. This doesn't mean that no more values are in the pipeline.s2
would still produce (and print) more values, but no one requests those values and thus execution stops.s4
again peeks at all values that pass it and prints them.forEach
consumes and prints whatevers4
passes to it.Think about it this way. The whole stream is completely lazy. Only the terminal operation actively pulls new values from the pipeline. After it has pulled 3 values from
s4 <- s3 <- s2 <- s1
,s3
will no longer produce new values and it will no longer pull any values froms2 <- s1
. Whiles1 -> s2
would still be able to produce4-9
, those values are just never pulled from the pipeline, and thus never printed bys2
.skip(6)
With
skip()
the same thing happens:s1
contains 9 differentInteger
values.s2
peeks at all values that pass it and prints them.s3
consumes the first 6 values, "skipping them", which means the first 6 values aren't passed tos4
, only the subsequent values are.s4
again peeks at all values that pass it and prints them.forEach
consumes and prints whatevers4
passes to it.The important thing here is that
s2
is not aware of the remaining pipeline skipping any values.s2
peeks at all values independently of what happens afterwards.Another example:
Consider this pipeline, which is listed in this blog post
When you execute the above, the program will never halt. Why? Because:
Which means:
i1
generates an infinite amount of alternating values:0
,1
,0
,1
,0
,1
, ...i2
consumes all values that have been encountered before, passing on only "new" values, i.e. there are a total of 2 values coming out ofi2
.i3
passes on 10 values, then stops.This algorithm will never stop, because
i3
waits fori2
to produce 8 more values after0
and1
, but those values never appear, whilei1
never stops feeding values toi2
.It doesn't matter that at some point in the pipeline, more than 10 values had been produced. All that matters is that
i3
has never seen those 10 values.To answer your question:
Nope. All operations before either
skip()
orlimit()
are executed. In both of your executions, you getA1
-A3
. Butlimit()
may short-circuit the pipeline, aborting value consumption once the event of interest (the limit is reached) has occurred.What you have here are two stream pipelines.
These stream pipelines each consist of a source, several intermediate operations, and a terminal operation.
But the intermediate operations are lazy. This means that nothing happens unless a downstream operation requires an item. When it does, then the intermediate operation does all it needs to produce the required item, and then again waits until another item is requested, and so on.
The terminal operations are usually "eager". That is, they ask for all the items in the stream that are needed for them to complete.
So you should really think of the pipeline as the
forEach
asking the stream behind it for the next item, and that stream asks the stream behind it, and so on, all the way to the source.With that in mind, let's see what we have with your first pipeline:
So, the
forEach
is asking for the first item. That means the "B"peek
needs an item, and asks thelimit
output stream for it, which meanslimit
will need to ask the "A"peek
, which goes to the source. An item is given, and goes all the way up to theforEach
, and you get your first line:The
forEach
asks for another item, then another. And each time, the request is propagated up the stream, and performed. But whenforEach
asks for the fourth item, when the request gets to thelimit
, it knows that it has already given all the items it is allowed to give.Thus, it is not asking the "A" peek for another item. It immediately indicates that its items are exhausted, and thus, no more actions are performed and
forEach
terminates.What happens in the second pipeline?
Again,
forEach
is asking for the first item. This is propagated back. But when it gets to theskip
, it knows it has to ask for 6 items from its upstream before it can pass one downstream. So it makes a request upstream from the "A"peek
, consumes it without passing it downstream, makes another request, and so on. So the "A" peek gets 6 requests for an item and produces 6 prints, but these items are not passed down.On the 7th request made by
skip
, the item is passed down to the "B" peek and from it to theforEach
, so the full print is done:Then it's just like before. The
skip
will now, whenever it gets a request, ask for an item upstream and pass it downstream, as it "knows" it has already done its skipping job. So the rest of the prints are going through the entire pipe, until the source is exhausted.It is complete blasphemy to look at steam operations individually because that is not how a stream is evaluated.
Talking about limit(3), it is a short circuit operation, which makes sense because thinking about it, whatever operation is before and after the
limit
, having a limit in a stream would stop iteration after getting n elements till the limit operation, but this doesn't mean that only n stream elements would be processed. Take this different stream operation for an examplewould output
which seem right, because limit is waiting for 3 stream elements to pass through the operation chain, although 6 elements of stream are processed.
All streams are based on spliterators, which have basically two operations: advance (move forward one element, similar to iterator) and split (divide oneself in arbitrary position, which is suitable for parallel processing). You can stop taking input elements at any moment you like (which is done by
limit
), but you cannot just jump to the arbitrary position (there's no such operation inSpliterator
interface). Thusskip
operation need to actually read the first elements from the source just to ignore them. Note that in some cases you can perform actual jump: