【问题标题】:How stream search, based on previous filter results, can be simplified?如何简化基于先前过滤结果的流搜索?
【发布时间】:2016-07-12 15:59:45
【问题描述】:

如何简化基于先前过滤结果的流搜索?

   HashMap<Boolean, List<Path>> result = (HashMap<Boolean, List<Path>>) Files.walk(Paths.get(folder)).map(Path
            ::toAbsolutePath).collect(Collectors.groupingBy(Files::isSymbolicLink));
    result.get(true).stream().filter(path -> {
        try {
            return !result.get(false).contains(Files.readSymbolicLink(path));
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
    }).forEach(symbolicLink -> {
        try {
            result.get(false).addAll(Files.walk(symbolicLink, FileVisitOption.FOLLOW_LINKS).collect(Collectors
                    .toList()));
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
    });

    return result.get(false).stream().filter(Files::isRegularFile).distinct().collect(Collectors.toList());

【问题讨论】:

    标签: java search recursion collections java-stream


    【解决方案1】:

    您可以通过将一些代码提取到方法中并避免使用HashMap 来整理它。

    您可以将文件存储在Set&lt;Path&gt; 中,而不是HashMap。这会强制执行唯一性,因此您实际上不需要检查符号链接是否指向您已经拥有的文件,并且您可以删除一些过滤:

    public Set<Path> getAllFiles(final String folder) throws IOException {
        return Files.walk(Paths.get(folder))
                .map(Path::toAbsolutePath)
                .flatMap(this::getAllFiles)
                .collect(Collectors.toSet());
    }
    
    private Stream<Path> getAllFiles(final Path path) {
        if(!Files.isSymbolicLink(path)) return Stream.of(path);
        try {
            return Files.walk(path, FileVisitOption.FOLLOW_LINKS)
                    .filter(Files::isRegularFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Stream.empty();
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-18
      • 2020-04-07
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 2019-09-10
      • 1970-01-01
      • 2021-03-08
      相关资源
      最近更新 更多