filterTree递归树过滤实现
递归 实现 过滤
2023-06-13 09:13:09 时间
二人同心,其利断金;同心之言,其臭如兰——《周易·系辞上》
引入依赖:
<!-- https://search.maven.org/artifact/io.github.vampireachao/stream-query -->
<dependency>
<groupId>io.github.vampireachao</groupId>
<artifactId>stream-core</artifactId>
<version>${stream-query-version}</version>
</dependency>
使用:
@Test
void testFilterTree() {
List<Student> studentTree = asList(
Student.builder().id(1L).name("dromara")
.children(asList(Student.builder().id(3L).name("hutool").parentId(1L)
.children(singletonList(Student.builder().id(6L).name("looly").parentId(3L).build()))
.build(),
Student.builder().id(4L).name("sa-token").parentId(1L)
.children(singletonList(Student.builder().id(7L).name("click33").parentId(4L).build()))
.build()))
.build(),
Student.builder().id(2L).name("baomidou")
.children(singletonList(
Student.builder().id(5L).name("mybatis-plus").parentId(2L)
.children(singletonList(
Student.builder().id(8L).name("jobob").parentId(5L).build()
))
.build()))
.build()
);
Assertions.assertEquals(singletonList(
Student.builder().id(1L).name("dromara")
.children(singletonList(Student.builder().id(3L).name("hutool").parentId(1L)
.children(singletonList(Student.builder().id(6L).name("looly").parentId(3L).build()))
.build()))
.build()), Steam.of(studentTree).filterTree(Student::getChildren, Student::setChildren, s -> "looly".equals(s.getName())).toList());
}
@Data
@Builder
public static class Student {
@Tolerate
public Student() {
// this is an accessible parameterless constructor.
}
private String name;
private Integer age;
private Long id;
private Long parentId;
private List<Student> children;
private Boolean matchParent;
}
相关文章
- 树形结构!别再用递归实现了,这才是最佳的方案;更快!更强!更好用!
- 编译原理文法详解_编译原理为什么存在递归文法
- 二叉树后序遍历的非递归实现_二叉树的后序遍历非递归详细
- mybatis之collection实现递归查询级联数据
- 用递归实现数组求和的函数_JAVA数组递归排序
- 爱上「递归」之链表反转
- C/C++递归实现全排列
- Oracle实现反向递归:揭秘(oracle反向递归)
- 查询MongoDB实现高效的递归查询(mongodb递归)
- 查询MSSQL使用递归查询实现数据管理(mssql递归)
- 递归路由简介
- Oracle实现多层递归查询的技巧(oracle上级递归查询)
- 探讨:C++实现链式二叉树(用非递归方式先序,中序,后序遍历二叉树)
- 对C语言中递归算法的深入解析
- c++递归实现n皇后问题代码(八皇后问题)
- php实例分享之通过递归实现删除目录下的所有文件详解
- Lua中实现递归删除一个文件夹
- php可应用于面包屑导航的递归寻找家谱树实现方法
- WinForm实现按名称递归查找控件的方法