stream实现递归封装
2023-06-13 09:11:02 时间
上帝等待着人类在智慧中获得新的童年。──泰戈尔
分享一个封装的树处理,源码在这:https://gitee.com/VampireAchao/stream-query
使用方式:
@Test
void testToTree() {
Consumer<Object> test = o -> {
List<Student> studentTree = Steam
.of(
Student.builder().id(1L).name("dromara").build(),
Student.builder().id(2L).name("baomidou").build(),
Student.builder().id(3L).name("hutool").parentId(1L).build(),
Student.builder().id(4L).name("sa-token").parentId(1L).build(),
Student.builder().id(5L).name("mybatis-plus").parentId(2L).build(),
Student.builder().id(6L).name("looly").parentId(3L).build(),
Student.builder().id(7L).name("click33").parentId(4L).build(),
Student.builder().id(8L).name("jobob").parentId(5L).build()
)
// just 3 lambda,top parentId is null
.toTree(Student::getId, Student::getParentId, Student::setChildren);
Assertions.assertEquals(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()
), studentTree);
};
test = test.andThen(o -> {
List<Student> studentTree = Steam
.of(
Student.builder().id(1L).name("dromara").matchParent(true).build(),
Student.builder().id(2L).name("baomidou").matchParent(true).build(),
Student.builder().id(3L).name("hutool").parentId(1L).build(),
Student.builder().id(4L).name("sa-token").parentId(1L).build(),
Student.builder().id(5L).name("mybatis-plus").parentId(2L).build(),
Student.builder().id(6L).name("looly").parentId(3L).build(),
Student.builder().id(7L).name("click33").parentId(4L).build(),
Student.builder().id(8L).name("jobob").parentId(5L).build()
)
// just 4 lambda ,top by condition
.toTree(Student::getId, Student::getParentId, Student::setChildren, Student::getMatchParent);
Assertions.assertEquals(asList(
Student.builder().id(1L).name("dromara").matchParent(true)
.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").matchParent(true)
.children(singletonList(
Student.builder().id(5L).name("mybatis-plus").parentId(2L)
.children(singletonList(
Student.builder().id(8L).name("jobob").parentId(5L).build()
))
.build()))
.build()
), studentTree);
});
test.accept(new Object());
}
@Test
void testFlatTree() {
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(asList(
Student.builder().id(1L).name("dromara").build(),
Student.builder().id(2L).name("baomidou").build(),
Student.builder().id(3L).name("hutool").parentId(1L).build(),
Student.builder().id(4L).name("sa-token").parentId(1L).build(),
Student.builder().id(5L).name("mybatis-plus").parentId(2L).build(),
Student.builder().id(6L).name("looly").parentId(3L).build(),
Student.builder().id(7L).name("click33").parentId(4L).build(),
Student.builder().id(8L).name("jobob").parentId(5L).build()
), Steam.of(studentTree).flatTree(Student::getChildren, Student::setChildren).sorted(Comparator.comparingLong(Student::getId)).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;
}
相关文章
- Android基于DataBinding封装RecyclerView实现快速列表开发
- 面向对象的三大特征-封装
- axios 上传文件 封装_使用axios上传文件,如何取消上传
- vue常用组件封装_vue组件全局注册和局部注册
- Allegro利用Skill实现BGA封装的自动扇出功能
- 【OkHttp】OkHttp 源码分析 ( 网络框架封装 | OkHttp 4 迁移 | OkHttp 建造者模式 )
- 【C++】红黑树封装实现 map 和 set
- 对Redis锁的简单封装
- TP5日志打印方法封装详解程序员
- Gson使用封装详解手机开发
- Linux 程序封装之路(linux程序封装)
- 开源新闻速递:snap 软件包封装工具 Snapcraft 2.9 发布
- 为自己而封装Redis从尝试到成功(自己封装redis)
- 分布式Redis封装最佳实践(分布式项目redis封装)
- 打造Redis高性能封装技术(怎么封装redis)
- python网页请求urllib2模块简单封装代码