List转换为tree-项目真实使用
【摘要】
前提:数据库中查出来每一条数据构成链表,需要我们转换成树结构,id,pid(父节点的id) name
主要代码:public Node getRoot(List<Node> list) 0为...
前提:数据库中查出来每一条数据构成链表,需要我们转换成树结构,id,pid(父节点的id) name
主要代码:public Node getRoot(List<Node> list)
0为根节点 1-9为次根节点 1.1-1.9 2.1-2.9 … 为次次根节点 看下方测试结果注意只有0的pid为-1
@RestController
public class AAA {
@RequestMapping("/project/test")
public Result get123(){
Test test = new Test();
List<Node> list = new ArrayList<>();
list.add(new Node("0","-1","根节点"));
list.add(new Node("9","0","9"));
list.add(new Node("9.1","9","9.1"));
list.add(new Node("1","0","1"));
list.add(new Node("1.1","1","1.1"));
list.add(new Node("2","0","2"));
list.add(new Node("2.1","2","2.1"));
list.add(new Node("3","0","3"));
list.add(new Node("3.1","3","3.1"));
list.add(new Node("3.1.2","3.1","3.1.2"));
Node root = test.getRoot(list);
Result<Object> result = new Result<>();
System.out.println(root);
result.setResult(root);
return result;
}
}
class Node{
String id;
String pid;
String name;
List<Node> children;
public Node(String id,String pid,String name){
this.id=id;
this.pid = pid;
this.name = name;
this.children = new ArrayList<>();
}
public List<Node> getChildren() {
return children;
}
public void setChildren(List<Node> children) {
this.children = children;
}
public void setId(String id){
this.id = id;
}
public void setPid(String pid) {
this.pid = pid;
}
public void setName(String name){
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getPid() {
return pid;
}
}
class Test{
public Node getRoot(List<Node> list){
Node root=null;
Map<String,Node> map= new HashMap<String,Node>();
for(Node n:list){
String id = n.getId();
Node node = map.get(id);
if(node==null){
node = n;
map.put(id,n);
}else{
node = map.get(id);
node.setPid(n.getPid());
node.setName(n.getName());
}
String pid = node.getPid();
if(pid.equals("-1")) { //根结点的判断
root = node;
continue;
}
Node node1 = map.get(pid);
if(node1==null){
node1 = new Node(pid,"-1","头节点");
map.put(pid,node1);
}
node1.getChildren().add(node);
}
return root;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
测试结果
{
"success": true,
"message": "操作成功!",
"code": 0,
"result": {
"id": "0",
"pid": "-1",
"name": "根节点",
"children": [
{
"id": "9",
"pid": "0",
"name": "9",
"children": [
{
"id": "9.1",
"pid": "9",
"name": "9.1",
"children": [
]
}
]
},
{
"id": "1",
"pid": "0",
"name": "1",
"children": [
{
"id": "1.1",
"pid": "1",
"name": "1.1",
"children": [
]
}
]
},
{
"id": "2",
"pid": "0",
"name": "2",
"children": [
{
"id": "2.1",
"pid": "2",
"name": "2.1",
"children": [
]
}
]
},
{
"id": "3",
"pid": "0",
"name": "3",
"children": [
{
"id": "3.1",
"pid": "3",
"name": "3.1",
"children": [
{
"id": "3.1.2",
"pid": "3.1",
"name": "3.1.2",
"children": [
]
}
]
}
]
}
]
},
"timestamp": 1651161878529
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
解释:
- 每次先查看map中是不是有id对应的key,如果有的话,说明之前添加过但是添加的对象只有id和children,这时候就需要把对应value中的对象的其他变量赋值,这个为什么只有id一会看到下边就知道了。
如果没有对应的key是id的话,添加key为id ,value为Node对象。 - 1中添加完id对应的Node之后,如果当前id节点的pid节点已存在,则把当前id节点添加到pid节点children数组中。如果不存在,需要先创建一个pid的新节点,并把新节点的id=pid,新节点添加到map中,并把1中的Node节点添加到新结点的children中(知道1中那个为什么只有id和children了吧)。
文章来源: englishcode.blog.csdn.net,作者:知识浅谈,版权归原作者所有,如需转载,请联系作者。
原文链接:englishcode.blog.csdn.net/article/details/124486600
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)