It works like option B, not necessarilly in that exact order, but more on that it does every operation on one element at a time.
The reasoning behind this is that variables only pass the stream once, so you need to perform all actions when you have that element right now, because once the element has passed, it is gone forever (from the stream's point of view).
Your code is, in a linear setting, very very very roughly equivalent to the following code, this is a very simplified version, but I hope you get the idea:
Collection<String> input = Arrays.asList("1", "2", "3");
Function<String, String> mapper = x -> x + " " + x;
Predicate<String> filter = y -> !y.startsWith("1");
Collector<String, ?, List<String>> toList = Collectors.toList();
List<String> list = ((Supplier<List<String>>)toList.supplier()).get();
for (String in : input) {
in = mapper.apply(in);
if (filter.test(in)) {
((BiConsumer<List<String>, String>)toList.accumulator()).accept(list, in);
}
}
What you see here, is:
- As input a
Collection<String>
, your input. - A
Function<String, String>
matching yourmap()
. - A
Predciate<String>
matching yourfilter()
. - A
Collector<String, ?, List<String>>
matching yourcollect()
, this is a collector that operates on elements of typeString
, uses intermediate storage?
and gives aList<String>
.
What it then does is:
- Obtain a new list, from the supplier (type:
Supplier<List<String>>
) of the collector. - Loop over every element of the input, done internally when operating on a
Stream<String>
, I am using aCollection<String>
here for expliciteness such that we still have a connection to the old Java 7 world. - Apply your mapping function.
- Test your filter predicate.
- Obtain the accumulator (type:
BiConsumer<List<String>, String>
) of thetoList
collector, this is the binary consumer that takes as arguments theList<String>
it already has, and theString
it wants to add. - Feed our
list
andin
to the accumulator.
Please take a very careful note that the real implementations is much much more advanced, as operations can happen in any order and multiple ones can happen, and much more.