private static List listToTree(List list, Long id) {
if (list == null || list.isEmpty()) {
return list;
}
Map<Long, Tree> treeMap = list.stream()
.collect(Collectors.toMap(Tree::getId, Function.identity()));
// 树的顶层栏目
Tree root = new Tree();
treeMap.put(id, root);
list.forEach(tree -> {
Long pid = tree.getPid();
Tree parent = treeMap.get(pid);
if (parent == null) {
log.error(“上层栏目分类不存在: {}”, JSONUtil.toJsonStr(tree));
return;
}
List children = parent.getChildren();
// 初始化子层级
if (children == null) {
children = new ArrayList<>();
parent.setChildren(children);
}
children.add(tree);
});
return root.getChildren();
}
下拉菜单树结构
最新推荐文章于 2024-07-23 17:35:39 发布