Java树结构(递归处理)
@Data
@Builder
public class Menu {
/**
* id
*/
public Integer id;
/**
* 名称
*/
public String name;
/**
* 父id ,根节点为0
*/
public Integer parentId;
/**
* 子节点信息
*/
public List<Menu> childList;
public Menu() {
}
public Menu(Integer id, String name, Integer parentId) {
this.id = id;
this.name = name;
this.parentId = parentId;
}
}
public class CovenLonki {
public static void main(String[] args) {
/*模拟从数据库查询出来*/
List<Menu> menus = Arrays.asList(
new Menu(1,"根节点1",0),
new Menu(111,"根节点2",0),
new Menu(2,"子节点1",1),
new Menu(6,"根节点2",1),
new Menu(11,"根节点3",1),
new Menu(3,"子节点1.1",2),
new Menu(4,"子节点1.2",2),
new Menu(5,"根节点1.3",2),
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(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(root.getId(), m.getParentId());
}).map(
m -> {
m.setChildList(getChildrens(m, all));
return m;
}
).collect(Collectors.toList());
return children;
}
}
运行结果:
-------转json输出结果-------
[{"name":"根节点1","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},{"name":"根节点2","childList":[],"id":111,"parentId":0}]
Java树结构(递归处理)
可用于 →→→ 对于树状结构化展示,嵌套循环,多层次结构.....
楼主不易,给我点赞就是给我最大的支持! {:1_893:}{:1_893:}{:1_893:} 运行结果:
-------转json输出结果-------
[
{
"name": "根节点1",
"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
},
{
"name": "根节点2",
"childList": [],
"id": 111,
"parentId": 0
}
] 这个是验证的吗? my2003sky 发表于 2023-8-15 14:16
这个是验证的吗?
运行运行就知道啦 这不是和那个谷粒商城里的一样嘛 以前还是玩过的,时间久了,就忘了,复习复习:lol 果断支持,楼主加油
页:
[1]