当前位置:网站首页>Return a tree structure data
Return a tree structure data
2022-07-03 02:11:00 【L1569850979】
Return a tree structure data
1、 Data structure display

2、 Text
package com.llcbk;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class NodeTest {
public static void main(String[]args){
// Prepare the data
List<TestNode> testNodeList = new ArrayList<>();
TestNode node1 = new TestNode(1,0," Parent node 1");
TestNode node2 = new TestNode(2,1," Child node ");
TestNode node3 = new TestNode(3,2," Child node 1.2");
TestNode node4 = new TestNode(4,0," Parent node 2");
TestNode node5 = new TestNode(5,4," Child node 2.2");
// Store data in a collection
testNodeList.add(node1);
testNodeList.add(node2);
testNodeList.add(node3);
testNodeList.add(node4);
testNodeList.add(node5);
List<TestNodeVo> bosUserVos = nodeDataToTree(testNodeList).get(String.valueOf(0)).getChildren();
System.out.println(" The data is :"+bosUserVos);
}
public static Map<String, TestNodeVo> nodeDataToTree(List<TestNode> list) {
String[] matchs = new String[list.size()];
String id;
String parentId;
int i = 0;
Map<String, TestNodeVo> map = new HashMap<>();
map.put(String.valueOf(0), new TestNodeVo());
map.get(String.valueOf(0)).setChildren(new ArrayList<>());
for (TestNode p : list) {
TestNodeVo vo = new TestNodeVo();
BeanUtils.copyProperties(p, vo);
map.put(String.valueOf(vo.getId()), vo);
if (vo.getChildren() == null) {
vo.setChildren(new ArrayList<TestNodeVo>());
}
if (vo.getParentId() == null || "".equals(vo.getParentId())) {
vo.setParentId(0);
}
matchs[i++] = vo.getId() + ":" + vo.getParentId();
}
for (String match : matchs) {
id = match.split(":")[0];
parentId = match.split(":")[1];
if (null != map.get(parentId)) {
if (null == map.get(parentId).getChildren()) {
map.get(parentId).setChildren(new ArrayList<>());
}
map.get(parentId).getChildren().add(map.get(id));
}
}
return map;
}
}
package com.llcbk;
import io.swagger.annotations.ApiModelProperty;
/** * node ( Used to generate tree relationships ) */
public class TestNode {
@ApiModelProperty(value = " Serial number ")
private Integer id;
@ApiModelProperty(value = " Parent node ")
private Integer parentId;
@ApiModelProperty(value = " The name of the node ")
private String nodeName;
public TestNode() {
}
public TestNode(Integer id, Integer parentId, String nodeName) {
this.id = id;
this.parentId = parentId;
this.nodeName = nodeName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
@Override
public String toString() {
return "TestNode{" +
"id=" + id +
", parentId=" + parentId +
", nodeName='" + nodeName + '\'' +
'}';
}
}
package com.llcbk;
import java.util.List;
public class TestNodeVo extends TestNode {
private List<TestNodeVo> children;
public List<TestNodeVo> getChildren() {
return children;
}
public void setChildren(List<TestNodeVo> children) {
this.children = children;
}
}
Breakpoint result display 
边栏推荐
- Socket programming
- Unrecognized SSL message, plaintext connection?
- [camera topic] complete analysis of camera dtsi
- Qt之QComboBox添加QCheckBox(下拉列表框插入复选框,含源码+注释)
- A 30-year-old software tester, who has been unemployed for 4 months, is confused and doesn't know what to do?
- Swift开发学习
- Network security - scan
- 2022 spring "golden three silver four" job hopping prerequisites: Software Test interview questions (with answers)
- Machine learning notes (constantly updating...)
- 技术大佬准备就绪,话题C位由你决定
猜你喜欢

His experience in choosing a startup company or a big Internet company may give you some inspiration
![[Flutter] dart: class;abstract class;factory;类、抽象类、工厂构造函数](/img/06/ab333a4752de27eae2dd937cf579e2.png)
[Flutter] dart: class;abstract class;factory;类、抽象类、工厂构造函数

Learn BeanShell before you dare to say you know JMeter

Huakaiyun (Zhiyin) | virtual host: what is a virtual host

Hard core observation 547 large neural network may be beginning to become aware?

可视化yolov5格式数据集(labelme json文件)

【Camera专题】Camera dtsi 完全解析

PyTorch 卷积网络正则化 DropBlock

通达OA 首页门户工作台
![[shutter] pull the navigation bar sideways (drawer component | pageview component)](/img/6f/dfc9dae5f890125d0cebdb2a0f4638.gif)
[shutter] pull the navigation bar sideways (drawer component | pageview component)
随机推荐
stm32F407-------ADC
SPI机制
RestCloud ETL 跨库数据聚合运算
[shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)
Introduce in detail how to communicate with Huawei cloud IOT through mqtt protocol
Network security OpenVAS
【Camera专题】HAL层-addChannel和startChannel简析
502 (bad gateway) causes and Solutions
elastic stack
His experience in choosing a startup company or a big Internet company may give you some inspiration
可視化yolov5格式數據集(labelme json文件)
The Sandbox阐释对元宇宙平台的愿景
Qt之QComboBox添加QCheckBox(下拉列表框插入复选框,含源码+注释)
技术大佬准备就绪,话题C位由你决定
Network security - phishing
2022 financial product revenue ranking
Redis:Redis的简单使用
Asian Games countdown! AI target detection helps host the Asian Games!
Use go language to realize try{}catch{}finally
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)