charles
public static void main(String[] args) {
final Stream<Integer> stream = Stream.of(1)
.peek(i -> System.out.println("outer stream"))
.flatMap(i -> {
return Stream.of(1)
.peek(i1 -> System.out.println("middle stream"))
.flatMap(i2 -> {
return Stream.of(1).peek(i3 -> System.out.println("inner stream"))
.onClose(() -> System.out.println("inner stream closed"));
})
.onClose(() -> System.out.println("middle stream closed"));
}).onClose(() -> System.out.println("outer stream closed"));
System.out.println("collection is about to start");
stream.collect(Collectors.toList());
System.out.println("collection is done");
}
charles
collection is about to start
outer stream
middle stream
inner stream
inner stream closed
middle stream closed
collection is done
the outer stream, while operated upon is never closed.Jared Rhizor (Airbyte)
Jared Rhizor (Airbyte)
charles
thisStreamMayNotBeClosed = Stream.of(1).flatMap(i -> butThisStreamWillBeClosed);
Jared Rhizor (Airbyte)
try
works, which makes sense to mecharles
Jared Rhizor (Airbyte)
close
is getting called in the flatmap caseJared Rhizor (Airbyte)
Jared Rhizor (Airbyte)
flatMap
does close streams for youJared Rhizor (Airbyte)
java.util.stream
Jared Rhizor (Airbyte)
Jared Rhizor (Airbyte)
java.util.stream.ReferencePipeline.flatMap
Jared Rhizor (Airbyte)
Jared Rhizor (Airbyte)
charles
charles
Jared Rhizor (Airbyte)
charles
Roshan George
12/15/2020, 12:24 AMcharles
Roshan George
12/15/2020, 12:25 AMcharles
charles
Roshan George
12/15/2020, 12:28 AMthisStreamMayNotBeClosed
you will close butThisStreamWillBeClosed
. That's a pretty clever use.charles
public static class StreamMaker {
public static Stream<Integer> makeStream() {
return Stream.of(1).onClose(() -> System.out.println("close a vital resource");
}
}
public static void main(String[] args) {
// external caller fails to call close!
StreamMaker.makeStream().collect(Collectors.toList());
}
charles
charles
public static class StreamMaker {
public static Stream<Integer> makeStream() {
return Stream.of(1).flatMap(i -> Stream.of(1).onClose(() -> System.out.println("close a vital resource"));
}
}
public static void main(String[] args) {
// external caller fails to call close, but it is okay because we are wrapped in a flatMap.
StreamMaker.makeStream().collect(Collectors.toList());
}
Roshan George
12/15/2020, 12:40 AMRoshan George
12/15/2020, 12:40 AMRoshan George
12/15/2020, 12:41 AMStream#close
existed.charles
Stream#close
. i didn't really realize it existed until the last couple years.Jared Rhizor (Airbyte)
post-terminal-operationin the original example there is no clearly terminal operation, which is one difficulty
Jared Rhizor (Airbyte)
Roshan George
12/15/2020, 12:51 AMStream#collect
is terminal, right?charles
charles
Roshan George
12/15/2020, 12:52 AMJared Rhizor (Airbyte)
Jared Rhizor (Airbyte)
Jared Rhizor (Airbyte)
Jared Rhizor (Airbyte)
Stream
could be using an underlying resource for some other methods even if the stream has been terminated? Therefore it doesn’t make sense to autoclose?