问题描述
在阅读 DirectoryStream
的API时,我错过了很多功能。首先,它建议使用for循环从流到 List
。我错过了 DirectoryStream
不是 Stream
的事实。
When reading the API for DirectoryStream
I miss a lot of functions. First of all it suggests using a for loop to go from stream to List
. And I miss the fact that it a DirectoryStream
is not a Stream
.
如何制作 Java 8中的java / nio / file / DirectoryStream.html> DirectoryStream
?
How can I make a Stream<Path>
from a DirectoryStream
in Java 8?
推荐答案
DirectoryStream
不是 Stream
(它自Java 7以来一直存在于流之前api是在Java 8中引入的,但是它实现了 Iterable< Path>
接口,所以你可以写:
DirectoryStream
is not a Stream
(it's been there since Java 7, before the streams api was introduced in Java 8) but it implements the Iterable<Path>
interface so you could write:
try (DirectoryStream<Path> ds = ...) {
Stream<Path> s = StreamSupport.stream(ds.spliterator(), false);
}
这篇关于如何从DirectoryStream创建流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!