前言:
树形结构在开发中还挺常见的,后端需要返回一个树形结构给前端,前端直接拿着这个树形结构展示在页面上,如何返回一个树形结构,一起随着小编看下去吧!
正文:
package com.test.nodefault;
import com.test.model.TestNode;
import com.test.model.vo.TestNodeVo;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author tanghh
* @Date 2020/3/13 19:22
*/
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;
}
}
讲解的几个点:
1.BeanUtils.copyProperties(p, vo); 将一个对象中相同的属性值复制到另外一个对象中。
2.思路就是首先先遍历所有的节点数据,生成id 和parent_id的关系,然后遍历id 和parent_id的关系,因此将子节点数据放入children 这个集合当中。
打个断点效果如下:
相当于是这么一个格式,这样的话,返回数据的时候就可以展示这个树形了。
附加部分
package com.test.model;
import io.swagger.annotations.ApiModelProperty;
/**
* @Author tanghh
* 节点(用来生成树形关系)
* @Date 2019/12/1 16:04
*/
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.test.model.vo;
import com.test.model.TestNode;
import java.util.List;
/**
* @Author tanghh
* @Date 2019/12/1 16:31
*/
public class TestNodeVo extends TestNode {
private List<TestNodeVo> children;
public List<TestNodeVo> getChildren() {
return children;
}
public void setChildren(List<TestNodeVo> children) {
this.children = children;
}
}
总结:
以上只是小编用到的一种,肯定还有更好的,
如果你觉得有更好的方法,欢迎评论区指出,一起加油!
来源:CSDN
作者:soup_tang
链接:https://blog.csdn.net/tangthh123/article/details/104843539