Carsten Varming varming at
Fri Jan 22 16:14:32 UTC 2016

Dear core-libs-devs,

I was playing around with streams and onClose and ran into an example with
odd behavior (well, I found it odd, see example below). I was wondering if
you could point me to prior discussions on semantics of intermediate
operations and the semantics of Stream.onClose.

A bit of code so we have something concrete to discuss:


public class Example {
  public static void main(String[] args) {
    final Stream<String> foo = Stream.of("1", "2", "3");
    final Stream<String> bar = foo.onClose(() ->
System.out.println("Closing bar"));

    try (final Stream<String> s = foo) {

When I run the program above I get:
Closing bar

Hmm, the onClose call mutated foo. This is surprising to me as when I read
[1] it says onClose is an intermediate operation and I quote: "Returns an
equivalent stream with an additional close handler.". The documentation
does not mention that the stream passed to onClose is mutated. Is this the
intended behavior or is it a bug? I hope it is a bug, but I would like to
understand the reasons for the current implementation.



More information about the core-libs-dev mailing list