When is a Java 8 Stream considered to be consumed?

后端 未结 2 1912
一整个雨季
一整个雨季 2021-01-07 20:15

My understanding was that a Java 8 Stream is considered to be consumed once a terminal operation, such as forEach() or count(), is per

相关标签:
2条回答
  • 2021-01-07 20:41

    According to the Stream Javadoc:

    A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may throw IllegalStateException if it detects that the stream is being reused. However, since some stream operations may return their receiver rather than a new stream object, it may not be possible to detect reuse in all cases.

    In your case, the call to filter itself is detecting an attempt to fork your stream into two different streams. Rather than wait and cause problems once the terminal operation is added, it's performing a pre-emptive strike to make it clear from any stack trace you get exactly where your problem is.

    0 讨论(0)
  • 2021-01-07 20:53

    A Stream is considered consumed once a terminal operation is executed. However, even multiple intermediate operations are not supposed to be executed for the same Stream instance, as stated in the Stream javadoc:

    A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may throw IllegalStateException if it detects that the stream is being reused. However, since some stream operations may return their receiver rather than a new stream object, it may not be possible to detect reuse in all cases.

    In case of intermediate stream operations, you should call the next operation on the Stream returned by the previous operation:

    public void multipleFilters_separate() {
        Stream<Double> ints = Stream.of(1.1, 2.2, 3.3);
        ints = ints.filter(d -> d > 1.3);
        ints.filter(d -> d > 2.3).forEach(System.out::println);
    }
    
    0 讨论(0)
提交回复
热议问题