Python 官方文档:入门教程 => 点击学习
目录一、前提二、代码三、使用1、实现Treenode接口2、使用3、样例一、前提 pId需要传入用来确认第一级的父节点,而且pId可以为null。树实体类必须实现:TreeNode接
定义TreeNode接口
public interface TreeNode {
String getId();
String getpId();
List getChildren();
}
需要将pId作为参数传入,在方法中添加一个pId参数,用于确认第一级的父节点。
public class TreeBuilder {
public static <T extends TreeNode> List<T> buildTree(List<T> nodes, String pId) {
List<T> roots = new ArrayList<>();
for (T node : nodes) {
if (node.getId().equals(pId) || (node.getParentId() == null && (pId == null || pId.equals(node.getId())))) {
roots.add(node);
}
}
for (T root : roots) {
buildChildren(root, nodes);
}
return roots;
}
private static <T extends TreeNode> void buildChildren(T node, List<T> nodes) {
for (T child : nodes) {
if (child.getParentId() != null && child.getParentId().equals(node.getId())) {
node.getChildren().add(child);
buildChildren(child, nodes);
}
}
}
}
构建到几级
public class TreeBuilder {
public static <T extends TreeNode> List<T> buildTree(List<T> nodes, String pId, int level) {
List<T> roots = new ArrayList<>();
for (T node : nodes) {
if (node.getId().equals(pId) || (node.getParentId() == null && (pId == null || pId.equals(node.getId())))) {
roots.add(node);
}
}
for (T root : roots) {
buildChildren(root, nodes, level, 1);
}
return roots;
}
private static <T extends TreeNode> void buildChildren(T node, List<T> nodes, int level, int currentLevel) {
if (currentLevel >= level) {
return;
}
for (T child : nodes) {
if (child.getParentId() != null && child.getParentId().equals(node.getId())) {
if (node.getChildren() == null) {
node.setChildren(new ArrayList<>());
}
node.getChildren().add(child);
buildChildren(child, nodes, level, currentLevel + 1);
}
}
}
}
public class MyTreeVo implements TreeNode {
private String id;
private String pId;
private List<MyTreeVo> children = Lists.newArrayList();
//其他属性……
public List<MyTreeVo> getChildren() {
return children;
}
public String getId() {
return id;
}
public String getpId() {
return pId;
}
//其他属性的getter、setter……
List<MyTreeVo> tree = TreeUtilQz.buildTree(vos,null);
//tree就是构建好的树结构数据
{
"data": [
{
"id": "e6ee51485389495cb923a122be800011",
"pId": "",
"name": "一、钢管钢管",
"children": [
{
"id": "e6ee51485389495cb923a122be800014",
"pId": "e6ee51485389495cb923a122be800011",
"name": "(二)嘎嘎嘎嘎嘎",
"children": []
},
{
"id": "e6ee51485389495cb923a122be800013",
"pId": "e6ee51485389495cb923a122be800011",
"name": "(一)顶顶顶顶",
"children": []
}
]
},
{
"id": "e6ee51485389495cb923a122be800012",
"pId": "",
"name": "二、噢噢噢噢",
"children": [
{
"id": "e6ee51485389495cb923a122be800015",
"pId": "e6ee51485389495cb923a122be800012",
"name": "二的下级",
"children": [
{
"id": "e6ee51485389495cb923a122be800016",
"pId": "e6ee51485389495cb923a122be800015",
"name": "二的下级的下级",
"children": []
}
]
}
]
}
]
}
到此这篇关于详解Java构建树结构的公共方法的文章就介绍到这了,更多相关Java构建树结构内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 详解Java构建树结构的公共方法
本文链接: https://lsjlt.com/news/209937.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0