当前位置:网站首页>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 
边栏推荐
- elastic stack
- Leetcode (540) -- a single element in an ordered array
- 【Camera专题】HAL层-addChannel和startChannel简析
- Kotlin middle process understanding and Practice (I)
- [camera topic] complete analysis of camera dtsi
- 全链路数字化转型下,零售企业如何打开第二增长曲线
- Startup mode and scope builder of collaboration in kotlin
- Cfdiv2 fixed point guessing- (interval answer two points)
- What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
- Detailed introduction to the deployment and usage of the Nacos registry
猜你喜欢

Solution for processing overtime orders (Overtime unpaid)

微信小程序開發工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理問題

A 30-year-old software tester, who has been unemployed for 4 months, is confused and doesn't know what to do?

Servlet中数据传到JSP页面使用el表达式${}无法显示问题

Learn BeanShell before you dare to say you know JMeter

Processing of tree structure data

stm32F407-------IIC通讯协议
![[Flutter] dart: class;abstract class;factory;类、抽象类、工厂构造函数](/img/06/ab333a4752de27eae2dd937cf579e2.png)
[Flutter] dart: class;abstract class;factory;类、抽象类、工厂构造函数

Comment communiquer avec Huawei Cloud IOT via le Protocole mqtt
![[camera topic] complete analysis of camera dtsi](/img/cb/d42589fcf0610600c9dc8c7992d4d7.png)
[camera topic] complete analysis of camera dtsi
随机推荐
[shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)
返回一个树形结构数据
Solution for processing overtime orders (Overtime unpaid)
疫情當頭,作為Leader如何進行團隊的管理?| 社區征文
How to deal with cache hot key in redis
[leetcode] 797 and 1189 (basis of graph theory)
Modify table structure
Comment le chef de file gère - t - il l'équipe en cas d'épidémie? Contributions communautaires
机器学习流程与方法
可视化yolov5格式数据集(labelme json文件)
Return the only different value (de duplication)
Su Shimin: 25 principles of work and life
require.context
Certaines fonctionnalités du développement d'applets
[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
Swift development learning
[fluent] hero animation (hero animation use process | create hero animation core components | create source page | create destination page | page Jump)
[Yu Yue education] reference materials of chemical experiment safety knowledge of University of science and technology of China
Kotlin middle process understanding and Practice (I)
PyTorch 卷积网络正则化 DropBlock