当前位置:网站首页>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 
边栏推荐
- The testing process that software testers should know
- Basic operation of view
- Query product cases - page rendering data
- iptables 4层转发
- 各国Web3现状与未来
- Analysis, use and extension of open source API gateway apisex
- What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
- 微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
- 通达OA 首页门户工作台
- Missing library while loading shared libraries: libisl so. 15: cannot open shared object file: No such file
猜你喜欢

In the face of difficult SQL requirements, HQL is not afraid

Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly

Learn BeanShell before you dare to say you know JMeter

Redis: simple use of redis

树形结构数据的处理

SPI机制

Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux

elastic stack

What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it

What are MySQL locks and classifications
随机推荐
2022 financial product revenue ranking
使用Go语言实现try{}catch{}finally
Explore the conversion between PX pixels and Pt pounds, mm and MM
The technology boss is ready, and the topic of position C is up to you
创建+注册 子应用_定义路由,全局路由与子路由
Cancellation of collaboration in kotlin, side effects of cancellation and overtime tasks
Visual yolov5 format data set (labelme JSON file)
Huakaiyun (Zhiyin) | virtual host: what is a virtual host
Swift开发学习
String replace space
疫情当头,作为Leader如何进行团队的管理?| 社区征文
stm32F407-------ADC
Servlet中数据传到JSP页面使用el表达式${}无法显示问题
Basic operation of view
Everything file search tool
2022 spring "golden three silver four" job hopping prerequisites: Software Test interview questions (with answers)
Comment communiquer avec Huawei Cloud IOT via le Protocole mqtt
Where is the future of test engineers? Confused to see
《上市风云》荐书——唯勇气最可贵
Swift development learning