当前位置:网站首页>List converted to tree real use of the project
List converted to tree real use of the project
2022-07-26 05:04:00 【On knowledge】
Premise : Each piece of data found in the database constitutes a linked list , We need to transform it into a tree structure ,id,pid( Parent node id) name
Main code :public Node getRoot(List<Node> list)
0 Root node 1-9 Is a secondary root node 1.1-1.9 2.1-2.9 … It is a secondary root node See the test results below Note that only 0 Of pid by -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"," The root node "));
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")) {
// Judgment of root node
root = node;
continue;
}
Node node1 = map.get(pid);
if(node1==null){
node1 = new Node(pid,"-1"," Head node ");
map.put(pid,node1);
}
node1.getChildren().add(node);
}
return root;
}
}
test result
{
"success": true,
"message": " Successful operation !",
"code": 0,
"result": {
"id": "0",
"pid": "-1",
"name": " The root node ",
"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
}
explain :
- Check first every time map Is there any id Corresponding key, If any , Note that the objects added before are only id and children, At this time, we need to correspond value Assignment of other variables of the object in , Why is there only id I'll see it later .
If there is no counterpart key yes id Words , add to key by id ,value by Node object . - 1 After adding id Corresponding Node after , If at present id Node pid Node already exists , Put the current id Add nodes to pid node children Array . If it doesn't exist , I need to create one first pid The new node , And put the new node id=pid, New nodes are added to map in , And put 1 Medium Node The node is added to the new node children in ( know 1 Why is there only id and children Is that right ).
边栏推荐
- 嵌入式分享合集20
- What are the demand management software for small and medium-sized enterprises
- 五个维度着手MySQL的优化,我和面试官都聊嗨了
- CountLaunch Demo的测试
- What is the difference between asynchronous and synchronous transmission signals (electronic hardware)
- Please elaborate on the implementation principle of synchronized and related locks
- 分子骨架跃迁工具-DeLinker介绍
- 【ACWing】2983. 玩具
- Date and time function of MySQL function summary
- Learn to map with nature medicine -- complex heat map
猜你喜欢

基于R语言的Meta分析【全流程、不确定性分析】方法与Meta机器学习

C language - pointer one touch ※

汉字风格迁移篇---通过生成对抗网络学习一对多程式化汉字的转换和生成

Alibaba three sides: how to solve the problems of MQ message loss, duplication and backlog?

分子骨架跃迁工具-DeLinker介绍

IEC61131 数据类型与 C#数据类型的对应

There was an unexpected error (type=method not allowed, status=405)

Seata两阶段提交AT详解

BigDecimal 的 4 个坑,你踩过几个?

JVM第五讲:纵横数据如何应对洪峰推送
随机推荐
What points should be paid attention to in the selection of project management system?
滑动窗口——leetcode题解
[mathematical modeling] basic knowledge of MATLAB
[acwing] 1268. Simple questions
创建MySQL数据库的两种方式
【pytorch】torch1.8.1安装、查看torch版本、GPU是否可用
columns in GROUP BY clause; this is incompatible with sql_ mode=only_ full_ group_ By mysql8.0 solution
npm操作指令
JVM Lecture 2: class loading mechanism
分子骨架跃迁工具-DeLinker介绍
What is the difference between asynchronous and synchronous transmission signals (electronic hardware)
A material of machine learning
Google Emoji guessing game helps parents guide their children to surf the Internet safely
Excel VBA:将多个工作表保存为新文件
JVM第五讲:纵横数据如何应对洪峰推送
一次线上事故,我顿悟了异步的精髓
C language -- string function, memory function collection and Simulation Implementation
YOLOv5执行全过程----目录
What is the real HTAP? (1) Background article
security权限管理详解