简体   繁体   English

在Java 8 Stream API中,DirectoryStream有什么区别 <Path> 和流 <Path> ?

[英]In Java 8 Stream API, what's the difference between a DirectoryStream<Path> and Stream<Path>?

I want to return a stream of paths (these are files located in a certain directory). 我想返回路径流(这些文件位于某个目录中)。 My initial approach was this: 我最初的方法是这样的:

DirectoryStream getFiles(Path dir) throws IOException {
    Files.newDirectoryStream(dir);
}

... but, I would like to know the difference between the above snippet and this second one: ...但是,我想知道以上代码段与第二个代码段之间的区别:

Stream<Path> getFiles(Path dir) throws IOException {
    Spliterator<Path> spl = Files.newDirectoryStream(dir).spliterator();
    return StreamSupport.stream(spl, false);
}

Both DirectoryStream and Stream are sub-interfaces of AutoCloseable , but beyond that, they seem to be designed for different purposes. DirectoryStreamStream都是AutoCloseable子接口,但除此之外,它们似乎是为不同的目的而设计的。

To be more precise, my question is this: 更确切地说,我的问题是:

What are the conceptual and functionality-based differences between DirectoryStream and Stream interfaces in Java-8? Java-8中DirectoryStreamStream接口之间在概念和基于功能上的区别是什么?

What are the conceptual and functionality-based differences between DirectoryStream and Stream interfaces in Java-8? Java-8中DirectoryStream和Stream接口之间在概念和基于功能上的区别是什么?

Java Stream API is general purpose API designed and implemented provide immutable, lazy, functional/declarative style of coding with any stream of objects. Java Stream API是设计和实现的通用API,可为任何对象流提供不可变的,惰性的,功能性/声明性的编码样式。 This is not specific for one scope and has mechanisms to filter, transform, aggregate data coming from the stream. 这不是特定于一个范围的,它具有过滤,转换和聚合来自流的数据的机制。

Where as DirectoryStream is specifically designed to cater loading, filtering and iterating through file system directories in a easy to use API. 其中,DirectoryStream是专门设计用于在易于使用的API中满足文件系统目录的加载,过滤和迭代的功能。

Java Stream API has clear cut common usage functions and corresponding SAM (Single Abstract Method) interfaces to ease coding for almost any usecase. Java Stream API具有明确的常用功能和相应的SAM(单一抽象方法)接口,可简化几乎所有用例的编码。

Where as DirectoryStream has convenient functions and interfaces to carry out loading, filtering, iterating over directories easy. 由于DirectoryStream具有便捷的功能和接口,因此可以轻松进行目录的加载,筛选和迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM