当前位置:网站首页>返回一个树形结构数据
返回一个树形结构数据
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;
}
}
断点结果展示
边栏推荐
- Machine learning notes (constantly updating...)
- stm32F407-------ADC
- [shutter] shutter debugging (debugging fallback function | debug method of viewing variables in debugging | console information)
- [camera topic] turn a drive to light up the camera
- [Yu Yue education] reference materials of chemical experiment safety knowledge of University of science and technology of China
- 詳細些介紹如何通過MQTT協議和華為雲物聯網進行通信
- [Yu Yue education] China Ocean University job search OMG reference
- 小程序開發的部分功能
- [shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
- 微服务组件Sentinel (Hystrix)详细分析
猜你喜欢
The technology boss is ready, and the topic of position C is up to you
Comment communiquer avec Huawei Cloud IOT via le Protocole mqtt
[leetcode] 797 and 1189 (basis of graph theory)
[shutter] pull the navigation bar sideways (drawer component | pageview component)
【Camera专题】手把手撸一份驱动 到 点亮Camera
Asian Games countdown! AI target detection helps host the Asian Games!
PyTorch 卷积网络正则化 DropBlock
深度学习笔记(持续更新中。。。)
In 2022, 95% of the three most common misunderstandings in software testing were recruited. Are you that 5%?
PS remove watermark details
随机推荐
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)
Prohibited package name
2022 spring "golden three silver four" job hopping prerequisites: Software Test interview questions (with answers)
Network security - DNS spoofing and phishing websites
[leetcode] 797 and 1189 (basis of graph theory)
Anna: Beibei, can you draw?
502 (bad gateway) causes and Solutions
What are MySQL locks and classifications
Redis: simple use of redis
去除网页滚动条方法以及内外边距
我的创作纪念日
Swift开发学习
疫情當頭,作為Leader如何進行團隊的管理?| 社區征文
Problems encountered in small program development of dark horse shopping mall
技术大佬准备就绪,话题C位由你决定
[camera topic] complete analysis of camera dtsi
A 30-year-old software tester, who has been unemployed for 4 months, is confused and doesn't know what to do?
stm32F407-------IIC通讯协议
When the epidemic comes, how to manage the team as a leader| Community essay solicitation
Network security OpenVAS