Stream-Query Stream-Query
首页
  • 项目初衷与诞生
  • 介绍
  • 快速上手
  • 目录结构
  • core模块

    • Opp
    • Sf
    • Map
    • Steam
    • TreeHelper
    • HighlightHelper
    • ReflectHelper
    • BeanHelper
  • plugin模块

    • Database
    • One
    • Many
    • OneToOne
    • OneToMany
  • 安装

    • 通过Maven安装
    • 通过Gradle安装
    • 下载jar安装
    • 通过源码编译
  • 动态mapper

    • 指定package
    • 指定注解
    • 指定接口
    • 指定实体类
  • 团队成员
  • dromara
  • 社区周边
  • 成为开发成员
  • 项目问题

    • 最新版本依赖下载失败
    • 项目编译报错
    • 如何贡献代码
    • 如何贡献文档
    • 使用本项目需要的前置知识
  • 其他问题

    • 作者是在全职维护开源吗
    • 你们这是个什么团队啊
    • 和其他类似的开源项目有什么区别
赞助
  • 1-5-0-alpha
  • 1-6-0-alpha
  • 1-7-0-alpha
  • 1-8-0
  • 1-9-0
  • 1-9-1
  • 2.0.0-alpha+001
  • 2.0.0-alpha+002
  • 2.0.2
Source (opens new window)
首页
  • 项目初衷与诞生
  • 介绍
  • 快速上手
  • 目录结构
  • core模块

    • Opp
    • Sf
    • Map
    • Steam
    • TreeHelper
    • HighlightHelper
    • ReflectHelper
    • BeanHelper
  • plugin模块

    • Database
    • One
    • Many
    • OneToOne
    • OneToMany
  • 安装

    • 通过Maven安装
    • 通过Gradle安装
    • 下载jar安装
    • 通过源码编译
  • 动态mapper

    • 指定package
    • 指定注解
    • 指定接口
    • 指定实体类
  • 团队成员
  • dromara
  • 社区周边
  • 成为开发成员
  • 项目问题

    • 最新版本依赖下载失败
    • 项目编译报错
    • 如何贡献代码
    • 如何贡献文档
    • 使用本项目需要的前置知识
  • 其他问题

    • 作者是在全职维护开源吗
    • 你们这是个什么团队啊
    • 和其他类似的开源项目有什么区别
赞助
  • 1-5-0-alpha
  • 1-6-0-alpha
  • 1-7-0-alpha
  • 1-8-0
  • 1-9-0
  • 1-9-1
  • 2.0.0-alpha+001
  • 2.0.0-alpha+002
  • 2.0.2
Source (opens new window)
  • 指南

    • 快速上手
    • 模块介绍
    • 项目初衷与诞生
  • core

    • Opp
    • Sf
    • TreeHelper
      • 此文档使用前置数据声明
      • of
      • toTree
      • toTree(构建层级)
      • getTreeByLevel()
      • cascadeSelect
        • 参数说明
        • 返回值
      • flat
      • filter
      • forEach
      • 测试用例地址
    • LighHight
    • LambdaHelper
    • Steam
    • Maps
    • ReflectHelper
    • BeanHelper
  • plugin

    • DataBase
    • One
    • Many
    • OneToOne
    • OneToMany
    • QueryCondition
    • safe-mode
  • 配置

  • 指南
  • core
臧臧
2023-04-18
目录

TreeHelper

# TreeHelper

# 此文档使用前置数据声明

originStudentList =
        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())
        .toList();
        originStudentTree =
        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());
        treeByLevelOriginStudentTree =
        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(),
        Student.builder()
        .id(5L)
        .name("mybatis-plus")
        .parentId(2L)
        .children(
        singletonList(Student.builder().id(8L).name("jobob").parentId(5L).build()))
        .build());
        treeFromRootToLevelOriginStudentTree =
        asList(
        Student.builder()
        .id(1L)
        .name("dromara")
        .matchParent(true)
        .children(
        asList(
        Student.builder().id(3L).name("hutool").parentId(1L).build(),
        Student.builder().id(4L).name("sa-token").parentId(1L).build()))
        .build(),
        Student.builder()
        .id(2L)
        .name("baomidou")
        .matchParent(true)
        .children(
        singletonList(
        Student.builder().id(5L).name("mybatis-plus").parentId(2L).build()))
        .build());
        studentTreeHelper =
        TreeHelper.of(
        Student::getId, Student::getParentId, null, Student::getChildren, Student::setChildren);
        }
        
        // 实体对象
        private static class Student {
            private String name;
            private Integer age;
            private Long id;
            private Long parentId;
            private List<Student> children;
            private Boolean matchParent;

            @Tolerate
            public Student() {
                // this is an accessible parameterless constructor.
            }
        }

# 使用讲解

# of

构建树先生来帮我们操作
参数说明:节点id,父节点id,父节点id值,获取子节点,操作子节点

studentTreeHelper = TreeHelper.of(Student::getId, Student::getParentId, null, Student::getChildren, Student::setChildren);

通过以上操作可以构建我们的树先生出来帮助我们做一些操作了是不是操作很简单呢 接下来看一些树先生可以帮你做的事情

# toTree

传入List集合通过创建树先生时所传入信息去构造树结构

List<Student> studentTree = studentTreeHelper.toTree(originStudentList);

# toTree(构建层级)

传入List集合通过创建树先生时所传入信息去构造树结构,并且通过level控制构建的层级

List<Student> studentTree = conditionTreeHelper.toTree(originStudentList, 1);

# getTreeByLevel()

获取树的指定层级所有节点(包含子节点)

TreeHelper<Student, Long> conditionTreeHelper =
        TreeHelper.ofMatch(
            Student::getId,
            Student::getParentId,
            s -> Boolean.TRUE.equals(s.getMatchParent()),
            Student::getChildren,
            Student::setChildren);
        // 结构相等
        treeByLevelOriginStudentTree 
        conditionTreeHelper.getTreeByLevel(originStudentList, 2)

# cascadeSelect

通过树结构进行级联选择(类似于前端级联选择器功能) 用于从一组树的根节点中查找到指定节点ID的完整路径。如果在任一树中找到该路径,该方法将返回路径;如果没有找到,则返回空列表。

# 参数说明

  • treeList: 树结构列表
  • id: 节点ID

# 返回值

  • 返回一个包含完整路径的列表,如果没有找到路径,则返回空列表
List<Student> studentTree = studentTreeHelper.toTree(originStudentList); // 转换树结构

Long targetStudentId = 6L;

List<Student> selectedPath = studentTreeHelper.cascadeSelect(studentTree, targetStudentId);

// selectedPath = [Student.builder().id(1L).name("dromara").matchParent(true).build(), Student.builder().id(3L).name("hutool").parentId(1L).build(), Student.builder().id(6L).name("looly").parentId(3L).build()]

# flat

将树结构进行扁平化

List<Student> studentList = studentTreeHelper.flat(originStudentTree);

# filter

根据给定的条件过滤列表中的元素,并且递归过滤子元素列表

List<Student> studentTree = studentTreeHelper.filter(originStudentTree, s -> "looly".equals(s.getName()));
/* 这样我们把包含这个果实的整个树枝都拿到了就,此时studentTree为 —>
        Student.builder().id(1L).name("dromara").matchParent(true)
            .children(singletonList(Student.builder().id(3L).name("hutool").parentId(1L)
                .children(singletonList(Student.builder().id(6L).name("looly").parentId(3L).build()))
                .build()))
            .build())
*/

# forEach

对列表中的元素以及它们的子元素列表进行递归遍历,并在每个元素上执行给定的操作

List<Student> studentList = studentTreeHelper.forEach(originStudentTree, s -> s.setName("【open source】" + s.getName()));

# 测试用例地址

TreeHelperTest (opens new window)

上次更新: 2024/04/25, 22:34:41
Sf
LighHight

← Sf LighHight→

Theme by Vdoing | Copyright © 2023-2024 阿超 | Apache License, Version 2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式