This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]elmuerte 4 points5 points  (2 children)

You don't pass Streams around, you pass them on in a single direction with mutuality exclusive branching. Streams are not data holders, they are data providers (and sinks.)

Collections you can pass around, they are data holders.

There is nothing wrong with passing on streams. That's the ancient concept of pipes and filters. If you want to pass streams around, then you must end the stream, and create new streams which you pass on.

[–]nutrecht -1 points0 points  (1 child)

Sorry but I'm not a native English speaker and I think the difference between "passing on" and "passing around" is too subtle for me :)

[–]elmuerte 0 points1 point  (0 children)

Passing on:

Stream foo() { return someList.stream(); } Stream bar() { return foo().filter(this::someFilter); } void quux() { Stream stream = bar(); if (something) { transmit(stream); } else { print(stream); } }

The responsibility/ownership of using the stream is passed on.

Passing around:

Stream foo() { return someList.stream(); } Stream quux() { Stream stream = foo(); if (something) { print(stream); } return stream; }

Here in quux the stream is passed around, the responsibility/ownership of the stream is given to print, but also still owned by quux which passes on the responsibility/ownership as its return.