stream.spliterator() 是否关闭了流?
是隐式关闭 了 ,还是之后需要显式关闭它?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
onClose()