2

我正在尝试使用以下 java 8 代码将aList函数“应用”到 a对象。ListFile

List<Function<File, String>> mappingFunctions = Arrays.asList(
    (file) -> file.getName(),
    (file) -> file.getPath(),
    (file) -> ((Long) file.length()).toString(),
    (file) -> file.isDirectory() ? "Directory" : "File"
);

// collects all files in this and one level down directory
List<File> files =
    Stream.of(new File(".").listFiles())
          .flatMap(file -> file.listFiles() == null ? Stream.of(file) : Stream.of(file.listFiles()))
          .collect(toList());

System.out.println("All the files gathered ...\n");
files.forEach(System.out::println);
System.out.println("\nApplying the list of functions to all the files gathered ...\n");
files.stream()
     .flatMap(file -> Stream.of(mappingFunctions.stream()
                                                .map((func) -> func.apply(file))
                                                .collect(Collectors.toList())))
     .forEach(System.out::println);

我得到的输出如下,

All the files gathered ...

.\.idea\compiler.xml
.\.idea\copyright
.\.idea\description.html
.\.idea\dictionaries
.\.idea\encodings.xml
.\.idea\misc.xml
.\.idea\modules.xml
.\.idea\project-template.xml
.\.idea\scopes
.\.idea\uiDesigner.xml
.\.idea\vcs.xml
.\.idea\workspace.xml
.\out\production
.\src\com
.\Test.iml

将函数列表应用于收集的所有文件......

[compiler.xml, .\.idea\compiler.xml, 739, File]
[copyright, .\.idea\copyright, 0, Directory]
[description.html, .\.idea\description.html, 97, File]
[dictionaries, .\.idea\dictionaries, 0, Directory]
[encodings.xml, .\.idea\encodings.xml, 164, File]
[misc.xml, .\.idea\misc.xml, 525, File]
[modules.xml, .\.idea\modules.xml, 255, File]
[project-template.xml, .\.idea\project-template.xml, 91, File]
[scopes, .\.idea\scopes, 0, Directory]
[uiDesigner.xml, .\.idea\uiDesigner.xml, 8792, File]
[vcs.xml, .\.idea\vcs.xml, 164, File]
[workspace.xml, .\.idea\workspace.xml, 28470, File]
[production, .\out\production, 0, Directory]
[com, .\src\com, 0, Directory]
[Test.iml, .\Test.iml, 437, File]

问题 1:为什么我在第二个输出中得到一个列表列表,而在第一个输出中只有一个列表?他们都在集合流上使用平面图。

问题 2:在 Java 8 中是否有更好的方法来实现相同的目标?

4

1 回答 1

1

In the second case the flatMap method produced a Stream<List<String>> whose single element is a List<String>.

In the first case the flatMap method either produces a Stream<File> of a single File or a Stream<File> of an array of Files.

Note that there are two Stream.of methods :

One accepts an array of type T (or a variable number of T arguments) and produces a Stream of T:

of(T... values)

This method is used when you write:

.flatMap(file -> Stream.of(file.listFiles()))

The other accepts a single T and produces a Stream with a single element T:

of(T t)

This method is used when you write:

.flatMap(file -> Stream.of(.. some expression that produces a List ..))

If you want your final Stream processing to produce a single Stream of Strings :

files.stream()
        .flatMap(file ->  mappingFunctions.stream()
                                          .map((func) -> func.apply(file)))
        .forEach(System.out::println);

This way, the lambda expression inside flatMap would produce a Stream<String> and flatMap would flatten those Streams into a single Stream<String>.

于 2015-02-19T09:49:07.490 回答