标签:上层 结构 str cto log tom 分类 栏目 etc
/**
* 栏目列表转成树结构
*
* @param list
* @return
*/
private static List<SysDict> listToTree(List<SysDict> list,Long id) {
if (list == null || list.isEmpty()) {
return list;
}
Map<Long, SysDict> sysDictMap = list.stream()
.collect(Collectors.toMap(SysDict::getId, Function.identity()));
// 树的顶层栏目
SysDict root= new SysDict();
sysDictMap.put(id, root);
list.forEach(sysDict -> {
Long pid = sysDict.getPid();
SysDict parent = sysDictMap.get(pid);
if (parent == null) {
log.error("上层栏目分类不存在: {}", JSONUtil.toJsonStr(sysDict));
return;
}
List<SysDict> children = parent.getChildren();
// 初始化子层级
if (children == null) {
children = new ArrayList<>();
parent.setChildren(children);
}
children.add(sysDict);
});
return root.getChildren();
}
标签:上层 结构 str cto log tom 分类 栏目 etc
原文地址:https://www.cnblogs.com/tank073/p/14304582.html