gpt4 book ai didi

java - 为什么不调用 java.util.stream.Stream#close()?

转载 作者:搜寻专家 更新时间:2023-11-01 02:44:48 25 4
gpt4 key购买 nike

当收集一个java.util.stream.Stream时,为什么它的方法void close()没有被调用?

最佳答案

根据 Stream javadoc ,

Streams have a BaseStream.close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files.lines(Path, Charset)) will require closing. Most streams are backed by collections, arrays, or generating functions, which require no special resource management. (If a stream does require closing, it can be declared as a resource in a try-with-resources statement.)

所以,听起来您需要使用 try-with-resources Statement .

关于java - 为什么不调用 java.util.stream.Stream#close()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25168660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com