List<TreeNode> nodes = new ArrayList<>();//替换成从数据库查询获得的list
Map<String, TreeNode> map = new HashMap<>();
List<TreeNode> parents = new ArrayList<>();
for (TreeNode node : nodes) {
map.put(node.getId(), node);
if (node.getParentId() == null) {
parents.add(node);
}
}
for (TreeNode node : nodes) {
TreeNode parent = map.get(node.getParentId());
if (parent != null) {
parent.getChildren().add(node);
}
}
return parents;
将从数据库查询出的父子关系数据 转换成树形结构
最新推荐文章于 2024-10-05 07:39:03 发布