当前位置:网站首页>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
边栏推荐
- Exception handling in kotlin process
- Return the only different value (de duplication)
- 各国Web3现状与未来
- 缺少库while loading shared libraries: libisl.so.15: cannot open shared object file: No such file
- RestCloud ETL 跨库数据聚合运算
- Network security - password cracking
- Kotlin middle process understanding and Practice (II)
- CFdiv2-Fixed Point Guessing-(區間答案二分)
- 深度(穿透)选择器 ::v-deep/deep/及 > > >
- Huakaiyun (Zhiyin) | virtual host: what is a virtual host
猜你喜欢
树形结构数据的处理
微服务组件Sentinel (Hystrix)详细分析
Recommendation letter of "listing situation" -- courage is the most valuable
Solution for processing overtime orders (Overtime unpaid)
[camera topic] how to save OTP data in user-defined nodes
Technology sharing | Frida's powerful ability to realize hook functions
通达OA 首页门户工作台
[camera topic] turn a drive to light up the camera
How do it students find short-term internships? Which is better, short-term internship or long-term internship?
Detailed introduction to the deployment and usage of the Nacos registry
随机推荐
[Yu Yue education] China Ocean University job search OMG reference
Leetcode 183 Customers who never order (2022.07.02)
Swift development learning
Solution for processing overtime orders (Overtime unpaid)
Custom components, using NPM packages, global data sharing, subcontracting
COM和CN
Missing library while loading shared libraries: libisl so. 15: cannot open shared object file: No such file
Caused by: com. fasterxml. jackson. databind. exc.MismatchedInputException: Cannot construct instance o
【Camera专题】手把手撸一份驱动 到 点亮Camera
MySQL learning 03
What are the key points often asked in the redis interview
转载收录6.5大侠写的部分Qt开发经验
Flink CDC mongoDB 使用及Flink sql解析monggo中复杂嵌套JSON数据实现
[shutter] top navigation bar implementation (scaffold | defaulttabcontroller | tabbar | tab | tabbarview)
Processing of tree structure data
Performance test | script template sorting, tool sorting and result analysis
Anna: Beibei, can you draw?
Visual yolov5 format data set (labelme JSON file)
Kotlin middle process understanding and Practice (II)
树形结构数据的处理