当前位置:网站首页>返回一个树形结构数据
返回一个树形结构数据
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;
}
}
断点结果展示
边栏推荐
- 詳細些介紹如何通過MQTT協議和華為雲物聯網進行通信
- iptables 4层转发
- Network security - phishing
- 可視化yolov5格式數據集(labelme json文件)
- DQL basic operation
- Use go language to realize try{}catch{}finally
- Certaines fonctionnalités du développement d'applets
- Leetcode 183 Customers who never order (2022.07.02)
- The technology boss is ready, and the topic of position C is up to you
- String replace space
猜你喜欢
微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
stm32F407-------DMA
LabVIEW安装第三方VISA软件后NI VISA失效
深度学习笔记(持续更新中。。。)
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)
Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
How to deal with cache hot key in redis
自定义组件、使用npm包、全局数据共享、分包
微服务组件Sentinel (Hystrix)详细分析
随机推荐
Performance test | script template sorting, tool sorting and result analysis
stm32F407-------ADC
【Camera专题】HAL层-addChannel和startChannel简析
Recommendation letter of "listing situation" -- courage is the most valuable
What are the key points often asked in the redis interview
[shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)
Where is the future of test engineers? Confused to see
Internal connection query and external connection
How to deal with cache hot key in redis
Network security - talking about security threats
Solution for processing overtime orders (Overtime unpaid)
Caused by: com. fasterxml. jackson. databind. exc.MismatchedInputException: Cannot construct instance o
easyPOI
Wechat applet development tool post net:: err_ PROXY_ CONNECTION_ Failed agent problem
小程序开发黑马购物商城中遇到的问题
Niuniu's ball guessing game (dynamic planning + prefix influence)
Network security - the simplest virus
浏览器是如何对页面进行渲染的呢?
Distributed transaction solution
What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it