当前位置:网站首页>返回一个树形结构数据
返回一个树形结构数据
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;
}
}
断点结果展示
边栏推荐
- 我的创作纪念日
- Processing of tree structure data
- Network security - password cracking
- Redis: simple use of redis
- Answers to ten questions about automated testing software testers must see
- 查询商品案例-页面渲染数据
- Solution for processing overtime orders (Overtime unpaid)
- Socket programming
- Missing library while loading shared libraries: libisl so. 15: cannot open shared object file: No such file
- Performance test | script template sorting, tool sorting and result analysis
猜你喜欢
![[leetcode] 797 and 1189 (basis of graph theory)](/img/2a/9c0a904151a17c2d23dea9ad04dbfe.jpg)
[leetcode] 797 and 1189 (basis of graph theory)

Performance test | script template sorting, tool sorting and result analysis

udp接收队列以及多次初始化的测试

技术大佬准备就绪,话题C位由你决定

Redis: simple use of redis

詳細些介紹如何通過MQTT協議和華為雲物聯網進行通信

What are MySQL locks and classifications
![[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)](/img/e7/915404743d6639ac359bb4e7f7fbb7.jpg)
[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)

Asian Games countdown! AI target detection helps host the Asian Games!

The technology boss is ready, and the topic of position C is up to you
随机推荐
创建+注册 子应用_定义路由,全局路由与子路由
What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
Network security - cracking system passwords
File class (check)
【Camera专题】手把手撸一份驱动 到 点亮Camera
Query product cases - page rendering data
【Camera专题】OTP数据如何保存在自定义节点中
How do it students find short-term internships? Which is better, short-term internship or long-term internship?
【Camera专题】HAL层-addChannel和startChannel简析
CFdiv2-Fixed Point Guessing-(区间答案二分)
Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)
Function definition and call, this, strict mode, higher-order function, closure, recursion
What are the key points often asked in the redis interview
小程序開發的部分功能
Missing library while loading shared libraries: libisl so. 15: cannot open shared object file: No such file
Network security - phishing
PS remove watermark details
Huakaiyun | virtual host: IP, subnet mask, gateway, default gateway
Recommendation letter of "listing situation" -- courage is the most valuable