1.创建树结构的类
@Data
public class TreeNode {
private Integer id;
private Integer parentId;
private String name;
private List<TreeNode> children;
}
2.构建树结构
public static List<TreeNode> getTree(){
//获取父节点
List<TreeNode> collect = trees.stream().filter(m -> m.getParentId() == 0).map(
(m) -> {
m.setChildren(getChildrenList(m, trees));
return m;
}
).collect(Collectors.toList());
return collect;
}
/**
* 获取子节点列表
* @param tree
* @param list
* @return
*/
public static List<TreeNode> getChildrenList(TreeNode tree, List<TreeNode> list){
List<TreeNode> children = list.stream().filter(item -> Objects.equals(item.getParentId(), tree.getId())).map(
(item) -> {
item.setChildren(getChildrenList(item, list));
return item;
}
).collect(Collectors.toList());
return children;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)