gpt4 book ai didi

Lambda 和递归的 java.lang.StackOverflowError 问题

转载 作者:行者123 更新时间:2023-12-01 10:07:28 27 4
gpt4 key购买 nike

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

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));
}

下面是堆栈跟踪

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)

最佳答案

你无限递归。如果 file 不是目录,您仍然会再次遍历它。首先检查 file 是否是一个目录,例如,

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

关于Lambda 和递归的 java.lang.StackOverflowError 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36347888/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com