返回一个树形结构数据
方式一:
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;
}
}
断点结果展示
方式二:
使用java8的流
1、数据结构展示
[
{
"name": "根节点",
"childList": [
{
"name": "子节点1",
"childList": [
{
"name": "子节点1.1",
"childList": [],
"id": 3,
"parentId": 2
},
{
"name": "子节点1.2",
"childList": [],
"id": 4,
"parentId": 2
},
{
"name": "根节点1.3",
"childList": [],
"id": 5,
"parentId": 2
}
],
"id": 2,
"parentId": 1
},
{
"name": "根节点2",
"childList": [
{
"name": "根节点2.1",
"childList": [
{
"name": "根节点2.2.1",
"childList": [],
"id": 9,
"parentId": 7
},
{
"name": "根节点2.2.2",
"childList": [],
"id": 10,
"parentId": 7
}
],
"id": 7,
"parentId": 6
},
{
"name": "根节点2.2",
"childList": [],
"id": 8,
"parentId": 6
}
],
"id": 6,
"parentId": 1
},
{
"name": "根节点3",
"childList": [
{
"name": "根节点3.1",
"childList": [],
"id": 12,
"parentId": 11
}
],
"id": 11,
"parentId": 1
}
],
"id": 1,
"parentId": 0
}
]
2、正文
实体类
package com.example.demo.testTwo.dto;
import lombok.Data;
import java.util.List;
@Data
public class Menu {
/**
* id
*/
public Integer id;
/**
* 名称
*/
public String name;
/**
* 父id ,根节点为0
*/
public Integer parentId;
/**
* 子节点信息
*/
public List<Menu> childList;
public Menu(Integer id, String name, Integer parentId) {
this.id = id;
this.name = name;
this.parentId = parentId;
}
public Menu(Integer id, String name, Integer parentId, List<Menu> childList) {
this.id = id;
this.name = name;
this.parentId = parentId;
this.childList = childList;
}
}
main方法
package com.example.demo.testTwo;
import com.alibaba.fastjson.JSON;
import com.example.demo.testTwo.dto.Menu;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @description 树形结构
*
* @author lilinchun
* @date 2024/05/14
*/
public class TestEight {
public static void main(String[] args) {
//模拟从数据库查询出来
List<Menu> menus = Arrays.asList(
new Menu(1, "根节点", 0),
new Menu(2, "子节点1", 1),
new Menu(3, "子节点1.1", 2),
new Menu(4, "子节点1.2", 2),
new Menu(5, "根节点1.3", 2),
new Menu(6, "根节点2", 1),
new Menu(7, "根节点2.1", 6),
new Menu(8, "根节点2.2", 6),
new Menu(9, "根节点2.2.1", 7),
new Menu(10, "根节点2.2.2", 7),
new Menu(11, "根节点3", 1),
new Menu(12, "根节点3.1", 11)
);
//获取父节点
List<Menu> collect = menus.stream().filter(m -> m.getParentId() == 0).map(
(m) -> {
m.setChildList(getChildrens(m, menus));
return m;
}
).collect(Collectors.toList());
System.out.println("-------转json输出结果-------");
System.out.println(JSON.toJSON(collect));
}
/**
* 递归查询子节点
* @param root 根节点
* @param all 所有节点
* @return 根节点信息
*/
private static List<Menu> getChildrens(Menu root, List<Menu> all) {
List<Menu> children = all.stream().filter(m -> {
return Objects.equals(m.getParentId(), root.getId());
}).map(
(m) -> {
m.setChildList(getChildrens(m, all));
return m;
}
).collect(Collectors.toList());
return children;
}
}