Java 8 Stream vs Collection Storage

前端 未结 5 1626
情深已故
情深已故 2021-02-02 01:38

I have been reading up on Java 8 Streams and the way data is streamed from a data source, rather than have the entire collection to extract data from.

This quote in par

5条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 01:59

    The statement about streams and storage means that a stream doesn't have any storage of its own. If the stream's source is a collection, then obviously that collection has storage to hold the elements.

    Let's take one of examples from that article:

    int sum = shapes.stream()
                    .filter(s -> s.getColor() == BLUE)
                    .mapToInt(s -> s.getWeight())
                    .sum();
    

    Assume that shapes is a Collection that has millions of elements. One might imagine that the filter operation would iterate over the elements from the source and create a temporary collection of results, which might also have millions of elements. The mapToInt operation might then iterate over that temporary collection and generate its results to be summed.

    That's not how it works. There is no temporary, intermediate collection. The stream operations are pipelined, so elements emerging from filter are passed through mapToInt and thence to sum without being stored into and read from a collection.

    If the stream source weren't a collection -- say, elements were being read from a network collection -- there needn't be any storage at all. A pipeline like the following:

    int sum = streamShapesFromNetwork()
                    .filter(s -> s.getColor() == BLUE)
                    .mapToInt(s -> s.getWeight())
                    .sum();
    

    might process millions of elements, but it wouldn't need to store millions of elements anywhere.

提交回复
热议问题