当前位置:网站首页>返回一个树形结构数据
返回一个树形结构数据
2022-07-03 01:54:00 【L1569850979】
返回一个树形结构数据
1、数据结构展示

2、正文
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){
//准备数据
List<TestNode> testNodeList = new ArrayList<>();
TestNode node1 = new TestNode(1,0,"父节点1");
TestNode node2 = new TestNode(2,1,"子节点");
TestNode node3 = new TestNode(3,2,"子节点1.2");
TestNode node4 = new TestNode(4,0,"父节点2");
TestNode node5 = new TestNode(5,4,"子节点2.2");
//将数据存储到集合中
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("数据为:"+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;
/** * 节点(用来生成树形关系) */
public class TestNode {
@ApiModelProperty(value = "序号")
private Integer id;
@ApiModelProperty(value = "父节点")
private Integer parentId;
@ApiModelProperty(value = "节点名称")
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;
}
}
断点结果展示
边栏推荐
- stm32F407-------ADC
- [shutter] bottom navigation bar implementation (bottomnavigationbar bottom navigation bar | bottomnavigationbaritem navigation bar entry | pageview)
- easyPOI
- Network security - scanning and password explosion 2
- Caused by: com. fasterxml. jackson. databind. exc.MismatchedInputException: Cannot construct instance o
- Where is the future of test engineers? Confused to see
- Niuniu's ball guessing game (dynamic planning + prefix influence)
- Reprint some Qt development experience written by great Xia 6.5
- 深度学习笔记(持续更新中。。。)
- Network security - virus
猜你喜欢

stm32F407-------IIC通讯协议

全链路数字化转型下,零售企业如何打开第二增长曲线

Machine learning notes (constantly updating...)

Stm32f407 ------- IIC communication protocol

可視化yolov5格式數據集(labelme json文件)

How to deal with cache hot key in redis

树形结构数据的处理

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

Solution for processing overtime orders (Overtime unpaid)

Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
随机推荐
His experience in choosing a startup company or a big Internet company may give you some inspiration
Network security - phishing
Iptables layer 4 forwarding
DDL basic operation
技术大佬准备就绪,话题C位由你决定
Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
Wechat applet development tool post net:: err_ PROXY_ CONNECTION_ Failed agent problem
使用Go语言实现try{}catch{}finally
Processing of tree structure data
Custom components, using NPM packages, global data sharing, subcontracting
Prohibited package name
Recommendation letter of "listing situation" -- courage is the most valuable
Performance test | script template sorting, tool sorting and result analysis
Network security - scanning and password explosion 2
What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
Huakaiyun (Zhiyin) | virtual host: what is a virtual host
ByteDance data Lake integration practice based on Hudi
小程序开发黑马购物商城中遇到的问题
MySQL learning 03
udp接收队列以及多次初始化的测试