当前位置:网站首页>返回一个树形结构数据
返回一个树形结构数据
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;
}
}
断点结果展示
边栏推荐
- MySQL学习03
- Leetcode(540)——有序数组中的单一元素
- 《上市风云》荐书——唯勇气最可贵
- Learn BeanShell before you dare to say you know JMeter
- Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
- Huakaiyun | virtual host: IP, subnet mask, gateway, default gateway
- Solution for processing overtime orders (Overtime unpaid)
- Where is the future of test engineers? Confused to see
- In 2022, 95% of the three most common misunderstandings in software testing were recruited. Are you that 5%?
- Explore the conversion between PX pixels and Pt pounds, mm and MM
猜你喜欢
Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)
微服务组件Sentinel (Hystrix)详细分析
Everything file search tool
Comment communiquer avec Huawei Cloud IOT via le Protocole mqtt
elastic stack
How to deal with cache hot key in redis
Button button adaptive size of wechat applet
Huakaiyun (Zhiyin) | virtual host: what is a virtual host
小程序開發的部分功能
微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
随机推荐
可視化yolov5格式數據集(labelme json文件)
Huakaiyun (Zhiyin) | virtual host: what is a virtual host
iptables 4层转发
Button button adaptive size of wechat applet
Depth (penetration) selector:: v-deep/deep/ and > > >
Certaines fonctionnalités du développement d'applets
Reprint some Qt development experience written by great Xia 6.5
Modify table structure
微信小程序開發工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理問題
机器学习笔记(持续更新中。。。)
Leetcode 183 Customers who never order (2022.07.02)
MySQL学习03
DDL basic operation
[shutter] shutter debugging (debugging fallback function | debug method of viewing variables in debugging | console information)
Trial setup and use of idea GoLand development tool
全链路数字化转型下,零售企业如何打开第二增长曲线
[shutter] top navigation bar implementation (scaffold | defaulttabcontroller | tabbar | tab | tabbarview)
Network security - cracking system passwords
DML Foundation
小程序开发黑马购物商城中遇到的问题