steam流递归遍历构建树形结构
作者:互联网
参考文章地址
:别再写一堆的 for 循环了!Java 8 中的 Stream 轻松遍历树形结构,是真的牛逼
较原文:增加了节点排序和省略部分无用代码
适用于:比如构建菜单
,展示文件树
,组织架构树
等
方案是:一次性从数据库把数据查出,通过递归遍历构建树形结构
01 菜单实体类
import lombok.Data;
import java.util.List;
/**
* @author : lyn
* 技术点 :
* @date : 2022/3/19 13:08
*/
@Data
public class Menu {
/**
* 父id
*/
private Long parentId;
/**
* id
*/
private Long id;
/**
* 排列序号
*/
private Integer orderNum;
/**
* 菜单名称
*/
private String menuName;
/**
* 子节点集合
*/
private List<Menu> childList;
public Menu(Long parentId, Long id, String menuName,Integer orderNum) {
this.parentId = parentId;
this.id = id;
this.menuName = menuName;
this.orderNum=orderNum;
}
public Menu setChildList(List<Menu> childList) {
this.childList = childList;
return this;
}
}
02 方法及测试
递归组装树形结构
import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author : lyn
* 技术点 :
* @description:
* @date : 2022/3/19 14:59
*/
@SpringBootTest
public class TestMenu {
@Test
public void testMenuShow() {
//准准备测试的基础数据
List<Menu> baseList = new ArrayList<>();
baseList.add(new Menu(0L, 1L, "根目录", 1));
baseList.add(new Menu(1L, 2L, "菜单管理", 1));
baseList.add(new Menu(2L, 3L, "添加菜单", 1));
baseList.add(new Menu(2L, 4L, "编辑菜单", 2));
baseList.add(new Menu(1L, 5L, "角色管理", 2));
baseList.add(new Menu(5L, 6L, "添加角色", 1));
baseList.add(new Menu(5L, 7L, "删除角色", 2));
baseList.add(new Menu(1L, 8L, "文件管理", 3));
baseList.add(new Menu(8L, 9L, "学习素材", 1));
baseList.add(new Menu(8L, 10L, "销售素材", 2));
baseList.add(new Menu(9L, 11L, "初高中", 1));
baseList.add(new Menu(9L, 12L, "婴幼儿", 2));
//递归组装树形结构
Menu root = baseList.stream()
.filter(s -> s.getParentId().equals(0L))
.map(m -> m.setChildList(getChildList(m.getId(), baseList)))
.collect(Collectors.toList())
.get(0);
String json = JSON.toJSONString(root);
System.out.println(json);
}
private List<Menu> getChildList(Long parentId, List<Menu> all) {
return all.stream()
.filter(s -> parentId.equals(s.getParentId()))
.map(m -> m.setChildList(getChildList(m.getId(), all)))
//排序
.sorted(Comparator.comparing(Menu::getOrderNum))
.collect(Collectors.toList());
}
}
03 树json串展示
{
"childList": [
{
"childList": [
{
"childList": [ ],
"id": 3,
"menuName": "添加菜单",
"orderNum": 1,
"parentId": 2
},
{
"childList": [ ],
"id": 4,
"menuName": "编辑菜单",
"orderNum": 2,
"parentId": 2
}
],
"id": 2,
"menuName": "菜单管理",
"orderNum": 1,
"parentId": 1
},
{
"childList": [
{
"childList": [ ],
"id": 6,
"menuName": "添加角色",
"orderNum": 1,
"parentId": 5
},
{
"childList": [ ],
"id": 7,
"menuName": "删除角色",
"orderNum": 2,
"parentId": 5
}
],
"id": 5,
"menuName": "角色管理",
"orderNum": 2,
"parentId": 1
},
{
"childList": [
{
"childList": [
{
"childList": [ ],
"id": 11,
"menuName": "初高中",
"orderNum": 1,
"parentId": 9
},
{
"childList": [ ],
"id": 12,
"menuName": "婴幼儿",
"orderNum": 2,
"parentId": 9
}
],
"id": 9,
"menuName": "学习素材",
"orderNum": 1,
"parentId": 8
},
{
"childList": [ ],
"id": 10,
"menuName": "销售素材",
"orderNum": 2,
"parentId": 8
}
],
"id": 8,
"menuName": "文件管理",
"orderNum": 3,
"parentId": 1
}
],
"id": 1,
"menuName": "根目录",
"orderNum": 1,
"parentId": 0
}
0 maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
标签:遍历,menuName,Menu,steam,orderNum,树形,parentId,childList,id 来源: https://www.cnblogs.com/lyn8100/p/16026896.html