Duplicating this discussion in our Java8 forum.
Go through the
methods of the Stream interface, and you will find they are described as terminal operations or intermediate operations (gross oversimplification warning
). Those labelled intermediate return a Stream, and those called terminal don't return a Stream; they might be void or return something different. Every time you call a method which is an intermediate operation, it creates a reference to a new Stream. That may seem expensive in memory, but it isn't. The Streams are probably created on the stack because you usually create them as local variables and they go out of scope as soon as the statement finishes. There is no point in having a Stream with more scope, becaus you cannot wind it back to its beginning and start again. You need a new Stream. Each Stream reference created will point to an object which executes one part of the action you want. If you execute yoru code on an
IDE like Eclipse, hover your mouse over each of the method calls and you will get a popup with the type of Stream, i.e. the type actually returned from each method call. That should allow you to see how the different Streams handle the data they are processing.
tim ledger wrote:
Let's reformat that code, with a slightly different indentation convention so all the dots align vertically:-
Now, if you have a Map<XYZ, Integer>, and you hover your mouse on the different lines the popups will say something like this:-
Line 1: Set<Map.Entry<XYZ, Integer>>Line 2: Stream<Map.Entry<XYZ, Integer>>Line 3: Stream<Map.Entry<XYZ, Integer>> Note the filer method is likely to return a Stream of exactly the same type.Line 4: Map<XYZ, Integer>>You will probably find that sort of information makes it easier to follow what is happening throughout the whole procedure. If you add a peek call anywhere, you will find that nothing happens until the collect method (which is a terminal operation) starts, because Streams use lazy execution.
If you go back to the Stream documentation, you will find the phrases like intermediate operation are hyperlinks and you will get more information if you click them.
[edit]I seem to have got the K and V types the wrong way round in what I wrote earlier. I shall leave it to the reader to work out how to correct my error. Sorry.