:trollface: 递归改为遍历

This commit is contained in:
VampireAchao 2022-09-19 11:53:58 +08:00
parent e81585d7d0
commit 0dda333b07

View File

@ -1,7 +1,6 @@
package cn.hutool.core.stream; package cn.hutool.core.stream;
import cn.hutool.core.lang.Opt; import cn.hutool.core.lang.Opt;
import cn.hutool.core.lang.mutable.MutableObj;
import cn.hutool.core.text.StrUtil; import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
@ -481,18 +480,14 @@ public class CollectorUtil {
final BiConsumer<T, List<T>> childrenSetter, final BiConsumer<T, List<T>> childrenSetter,
final boolean isParallel) { final boolean isParallel) {
return pIdValuesMap -> { return pIdValuesMap -> {
final MutableObj<Consumer<List<T>>> recursiveRef = new MutableObj<>(); EasyStream.of(pIdValuesMap.values(), isParallel).flat(Function.identity())
final Consumer<List<T>> recursive = parents -> EasyStream.of(parents, isParallel).forEach(parent -> { .forEach(value -> {
final List<T> children = pIdValuesMap.get(idGetter.apply(parent)); final List<T> children = pIdValuesMap.get(idGetter.apply(value));
childrenSetter.accept(parent, children); if (children != null) {
recursiveRef.get().accept(children); childrenSetter.accept(value, children);
}); }
final List<T> parents = parentFactory.apply(pIdValuesMap); });
if (false == parents.isEmpty()) { return parentFactory.apply(pIdValuesMap);
recursiveRef.set(recursive);
recursiveRef.get().accept(parents);
}
return parents;
}; };
} }