前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >stream实现递归封装

stream实现递归封装

作者头像
阿超
发布2022-09-23 16:04:57
2540
发布2022-09-23 16:04:57
举报
文章被收录于专栏:快乐阿超

上帝等待着人类在智慧中获得新的童年。──泰戈尔

分享一个封装的树处理,源码在这:https://gitee.com/VampireAchao/stream-query

使用方式:

代码语言:javascript
复制

@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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-08-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档