RFR: JDK-8277095 : Empty streams create too many objects

kabutz duke at openjdk.java.net
Mon Nov 15 12:46:14 UTC 2021


On Sat, 6 Nov 2021 17:23:34 GMT, Pavel Rappo <prappo at openjdk.org> wrote:

> Streams are closeable, and a terminal operation may be invoked on a given stream only once. Thus, shouldn't the third line in both of the examples below throw `IllegalStateException`?
> 
> ```
>     Stream<Object> empty = Stream.empty();
>     System.out.println(empty.count());
>     System.out.println(empty.count());
> 
>     Stream<Object> empty = Stream.empty();
>     empty.close();
>     System.out.println(empty.count());
> ```

That would be fairly easy to solve by having two instances of the EmptyStream. The terminal operations would return the terminal operation that throws IllegalStateExceptions.

-------------

PR: https://git.openjdk.java.net/jdk/pull/6275


More information about the core-libs-dev mailing list