类 TreeUtils

java.lang.Object
io.github.lishangbu.orange.web.util.TreeUtils

public class TreeUtils extends Object
树形结构工具类,用于处理树形数据

提供构建/查找/过滤/遍历等常用树操作,要求调用方显式传入获取子节点的函数,移除反射兼容以提高类型安全和性能

从以下版本开始:
2025/08/25
作者:
lishangbu
  • 构造器详细资料

    • TreeUtils

      public TreeUtils()
  • 方法详细资料

    • buildTree

      public static <T,I> List<T> buildTree(List<T> list, Function<T,I> idGetter, Function<T,I> parentIdGetter, Function<T,List<T>> childrenGetter, BiConsumer<T,List<T>> childrenSetter)
      将列表转换为树形结构,根父ID默认为 null
      另请参阅:
    • buildTree

      public static <T,I> List<T> buildTree(List<T> list, Function<T,I> idGetter, Function<T,I> parentIdGetter, Function<T,List<T>> childrenGetter, BiConsumer<T,List<T>> childrenSetter, I rootParentId)
      将列表转换为树形结构,允许指定 childrenGetter 和 rootParentId
      类型参数:
      T - 节点类型
      I - ID类型
      参数:
      list - 待转换的列表
      idGetter - 获取节点ID的函数
      parentIdGetter - 获取父节点ID的函数
      childrenGetter - 获取节点子列表的函数(比反射更高效、类型安全)
      childrenSetter - 设置子节点的函数
      rootParentId - 被视为根节点父ID的值(例如 null、0、-1 等)
      返回:
      树形结构的根节点列表
    • findNode

      public static <T> T findNode(List<T> tree, Predicate<T> predicate, Function<T,List<T>> childrenGetter)
      在树中查找符合条件的第一个节点
      类型参数:
      T - 节点类型
      参数:
      tree - 树结构
      predicate - 匹配条件
      childrenGetter - 获取子节点的函数
      返回:
      找到的节点,没有则返回null
    • findNodes

      public static <T> List<T> findNodes(List<T> tree, Predicate<T> predicate, Function<T,List<T>> childrenGetter)
      在树中查找所有符合条件的节点
      类型参数:
      T - 节点类型
      参数:
      tree - 树结构
      predicate - 匹配条件
      childrenGetter - 获取子节点的函数
      返回:
      找到的节点列表
    • getNodePath

      public static <T> List<T> getNodePath(List<T> tree, Predicate<T> targetPredicate, Function<T,List<T>> childrenGetter)
      获取从根节点到目标节点的路径
      类型参数:
      T - 节点类型
      参数:
      tree - 树结构
      targetPredicate - 目标节点匹配条件
      childrenGetter - 获取子节点的函数
      返回:
      路径节点列表,如果找不到目标节点则返回空列表
    • flattenTree

      public static <T> List<T> flattenTree(List<T> tree, Function<T,List<T>> childrenGetter)
      树形结构扁平化为列表
      类型参数:
      T - 节点类型
      参数:
      tree - 树结构
      childrenGetter - 获取子节点的函数
      返回:
      扁平化后的列表
    • filterTree

      public static <T> List<T> filterTree(List<T> tree, Predicate<T> predicate, Function<T,List<T>> childrenGetter, BiConsumer<T,List<T>> childrenSetter)
      过滤树节点,保持树形结构
      类型参数:
      T - 节点类型
      参数:
      tree - 树结构
      predicate - 过滤条件
      childrenGetter - 获取子节点的函数
      childrenSetter - 设置子节点的函数
      返回:
      过滤后的树结构
    • traverseTree

      public static <T> void traverseTree(List<T> tree, BiConsumer<T,Integer> action, Function<T,List<T>> childrenGetter)
      遍历树结构,对每个节点执行操作
      类型参数:
      T - 节点类型
      参数:
      tree - 树结构
      action - 要执行的操作
      childrenGetter - 获取子节点的函数
    • getMaxDepth

      public static <T> int getMaxDepth(List<T> tree, Function<T,List<T>> childrenGetter)
      计算树的最大深度
      类型参数:
      T - 节点类型
      参数:
      tree - 树结构
      childrenGetter - 获取子节点的函数
      返回:
      树的最大深度
    • findNodeById

      public static <T,I> T findNodeById(List<T> tree, I id, Function<T,I> idGetter, Function<T,List<T>> childrenGetter)
      根据节点ID查找节点
      类型参数:
      T - 节点类型
      I - ID类型
      参数:
      tree - 树结构
      id - 目标节点ID
      idGetter - 获取节点ID的函数
      childrenGetter - 获取子节点的函数
      返回:
      找到的节点,没有则返回null