Lamba和递归

时间:2016-04-01 04:10:32

标签: java recursion lambda

当我使用旧时尚list.iterator()在目录上进行递归时,下面的代码可以工作,但我不理解stackoverflow的原因,只有当我按照Java 8样式使用lambda时才会出现。

private void walk(File file, int depth) {
    if (depth >= maxDepth)
        return;     
    List<File> files = file.isDirectory()?Arrays.asList(file.listFiles()):Arrays.asList(file);
    filesStream.addAll(files.stream()
                        .filter(predicate1.and(predicate2))
                        .collect(Collectors.toList()));
    Stream<File> filteredDirectories = files.stream()
                                            .filter(predicate3.and(predicate4));
    int currentDepth = ++depth;
    filteredDirectories.forEach(f -> walk(f, currentDepth));
}   

下面是stacktrace

java.lang.StackOverflowError
at java.util.Collection.stream(Collection.java:581)
at org.util.DirectoryManager.walk(DirectoryManager.java:192)
at org.util.DirectoryManager.lambda$12(DirectoryManager.java:198)
at org.util.DirectoryManager$$Lambda$10/1068824137.accept(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.util.DirectoryManager.walk(DirectoryManager.java:198)
at org.util.DirectoryManager.lambda$12(DirectoryManager.java:198)
at org.util.DirectoryManager$$Lambda$10/1068824137.accept(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.util.DirectoryManager.walk(DirectoryManager.java:198)
at org.util.DirectoryManager.lambda$12(DirectoryManager.java:198)
at org.util.DirectoryManager$$Lambda$10/1068824137.accept(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
...
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.util.DirectoryManager.walk(DirectoryManager.java:198)
at org.util.DirectoryManager.lambda$12(DirectoryManager.java:198)
at org.util.DirectoryManager$$Lambda$10/1068824137.accept(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)

1 个答案:

答案 0 :(得分:1)

你无限地追逐。如果file不是目录,您仍然可以再次使用它。首先检查file是否为目录,如

if (file.isDirectory()) {
    Stream<File> filteredDirectories = files.stream()
            .filter(predicate1.and(predicate2));
    int currentDepth = ++depth;
    filteredDirectories.forEach(f -> walk(f, currentDepth));
}