当前位置:网站首页>List转换为tree-项目真实使用
List转换为tree-项目真实使用
2022-07-26 04:57:00 【知识浅谈】
前提:数据库中查出来每一条数据构成链表,需要我们转换成树结构,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;
}
}
测试结果
{
"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
}
解释:
- 每次先查看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了吧)。
边栏推荐
- Recognized again | saining network security has been listed in the ccsip 2022 panorama of China's network security industry
- IEC61131 数据类型与 C#数据类型的对应
- Switch and router technology: dynamic routing protocol, rip routing protocol and OSPF routing protocol
- 十一、异常处理器
- UE4 switching of control rights of multiple roles
- What are the restrictions on opening futures accounts? Where is the safest place to open an account?
- 奥特学园ROS笔记--6
- Embedded practice -- CPU utilization statistics based on rt1170 FreeRTOS (24)
- 数据库启动报:ORA-29702: error occurred in Cluster Group Service
- 九、文件上传和下载
猜你喜欢

9 best project set management tools

C language -- string function, memory function collection and Simulation Implementation

计算离散点的曲率(matlab)

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

Yapi installation

To study the trend of open source and gain insight into the future of the industry, stonedb community and the China Academy of communications and communications released the Research Report on the dev

What points should be paid attention to in the selection of project management system?

Use field parameters for report translation

【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(八)

Switch to router technology: OSPF single zone configuration, OSPF multi zone and end zone
随机推荐
[enterprise micro procedure]
Several maturity levels of using MES management system
[uoj 429] runs (inclusive) + a little record about Lyndon tree and its application
2022河南萌新联赛第(三)场:河南大学 L - 合成游戏
New knowledge in big homework
Kubernetes advanced training camp scheduler
Icml2022 | imitation learning by evaluating the professional knowledge of the presenter
Face database collection summary
Codeforces Round #807 (Div. 2)
擅长C(暑假每日一题 6)
AXI协议(5):AXI协议的burst机制
自动化测试框架该如何搭建?
UE4 displays text when it is close to the object, and disappears when it is far away
"Game engine light in light out" 4. shader
阿里云工业视觉智能工程师ACP认证——备考
Codeforces Round #807 (Div. 2)
SQL encryption and decryption injection details
9、 File upload and download
mongoDB为什么快
11、 Exception handler