preface :

Tree structure is very common in development , The back end needs to return a tree structure to the front end , The front end directly displays the tree structure on the page , How to return a tree structure , Let's watch it with Xiaobian !

text :
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){ // Prepare the data List<TestNode>
testNodeList = new ArrayList<>(); TestNode node1 = new TestNode(1,0," Parent node 1");
TestNode node2 = new TestNode(2,1," Child node "); TestNode node3 = new
TestNode(3,2," Child node 1.2"); TestNode node4 = new TestNode(4,0," Parent node 2"); TestNode
node5 = new TestNode(5,4," Child node 2.2"); // Store data in a collection 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(" The data is :"+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; } }
Several points of explanation :

1.BeanUtils.copyProperties(p, vo);    Copy the same property value from one object to another .

2. The idea is to first traverse all the node data , generate id and parent_id The relationship between , Then traverse id and parent_id The relationship between , Therefore, the child node data is put into the children
In this collection .

The effect of a breakpoint is as follows :

It's equivalent to such a format , In this case , The tree can be displayed when the data is returned . 

Additional part
package com.test.model; import io.swagger.annotations.ApiModelProperty; /** *
@Author tanghh * node ( Used to generate tree relationships ) * @Date 2019/12/1 16:04 */ public class TestNode
{ @ApiModelProperty(value = " Serial number ") private Integer id; @ApiModelProperty(value =
" Parent node ") private Integer parentId; @ApiModelProperty(value = " Node name ") 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; } }
 

summary :

The above is just a kind used by Xiaobian , There must be something better ,

If you think there's a better way , Welcome comments , Let's come on together !

Technology
©2019-2020 Toolsou All rights reserved,
QT Delete directory and file Java aggregate ------LinkedHashMap Underlying principle I've been drinking soft water for three years ? What is the use of soft water and water softener Java Xiao Ming A+B About wechat payment body( Product description ) Problems encountered Digital rolling lottery program 3 species Python data structure ,13 Creation methods , This is the conclusion , Great ! Huawei Mate 40 Pro+ 5G exposure : Leica film lens , Ceramic body What's the difference in server prices ? Operators Website visitors' mobile phone number capture