A statement about threads and storage means that the stream does not have its own storage. If the source of the stream is a collection, then it is obvious that the collection has storage for storing items.
Take one example from this article:
int sum = shapes.stream() .filter(s -> s.getColor() == BLUE) .mapToInt(s -> s.getWeight()) .sum();
Suppose shapes is a Collection that has millions of elements. It can be assumed that the filter operation will iterate over the elements from the source and create a temporary result set that can also have millions of elements. The mapToInt operation can then mapToInt over this temporary collection and generate its results for summation.
This is not how it works. There is no temporary, intermediate collection. The operations of the stream are pipelined, so the elements that exit the filter are passed through mapToInt and from here to sum without saving and reading from the collection.
If the stream source was not a collection โ say, elements were read from a network collection โ there should be no storage at all. The conveyor is as follows:
int sum = streamShapesFromNetwork() .filter(s -> s.getColor() == BLUE) .mapToInt(s -> s.getWeight()) .sum();
can handle millions of items, but it doesn't need to store millions of items anywhere.
Stuart Marks Apr 22 '15 at 17:36 2015-04-22 17:36
source share