首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >filterTree递归树过滤实现

filterTree递归树过滤实现

作者头像
阿超
发布2022-11-18 16:10:32
发布2022-11-18 16:10:32
5670
举报
文章被收录于专栏:快乐阿超快乐阿超

二人同心,其利断金;同心之言,其臭如兰——《周易·系辞上》

引入依赖:

代码语言:javascript
复制
<!-- 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>

使用:

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

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

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

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

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