是stream.spliterator()
隐式关闭stream
,还是需要在之后显式关闭它?
Stream<String> stream = Stream.of("a", "b", "c");
Spliterator<T> spliterator = stream.spliterator();
// Some low lever operation with the spliterator
stream.close(); // do we need to close?
乍一看,该.spliterator()
方法似乎关闭了stream
,但没有调用stream.close()
。至少如果我在.spliterator()
调用该方法后立即关闭它,它似乎不会影响拆分器操作。
Stream<String> stream = Stream.of("a", "b", "c").limit(2);
Spliterator<T> spliterator = stream.spliterator();
stream.close();
// Some low lever operation with the spliterator
这个问题可以扩展到其他stream
方法,例如.findAny()
.
stream.findAny() // Can I assume that I don't need to close the stream?
stream.onClose(() -> System.out.println("hi!")).findAny()`
// when the `onClose()` action will be called?
提出这个问题的原因是要明确何时stream
需要明确关闭 a ,以及在我不需要明确关闭它的情况下,onClose()
定义的操作何时发生?