Class ConcurrentTreeBuilder

java.lang.Object
cloud.opencode.base.tree.builder.ConcurrentTreeBuilder

public final class ConcurrentTreeBuilder extends Object
Concurrent Tree Builder 并发树构建器

Thread-safe tree builder for concurrent scenarios.

用于并发场景的线程安全树构建器。

Features | 主要功能:

  • Thread-safe tree construction using ConcurrentHashMap and synchronized blocks - 使用ConcurrentHashMap和synchronized块的线程安全树构建
  • Parallel stream processing for large node lists - 使用并行流处理大量节点列表
  • Automatic root detection (null/zero/empty parentId) - 自动根节点检测(null/零/空parentId)
  • Threshold-based sequential fallback for small lists - 小列表的基于阈值的顺序降级处理

Usage Examples | 使用示例:

// Build tree concurrently - 并发构建树
List<MyNode> roots = ConcurrentTreeBuilder.build(flatList);

// Build with explicit root ID - 使用显式根ID构建
List<MyNode> roots = ConcurrentTreeBuilder.build(flatList, 0L);

// Build large tree with parallel threshold - 带并行阈值的大型树构建
List<MyNode> roots = ConcurrentTreeBuilder.buildLarge(flatList, 0L, 1000);

Security | 安全性:

  • Thread-safe: Yes (uses ConcurrentHashMap and per-parent synchronization) - 线程安全: 是(使用ConcurrentHashMap和每父节点同步)
  • Null-safe: Partial (null input list returns empty list) - 空值安全: 部分(null输入列表返回空列表)

Performance | 性能特性:

  • Time complexity: O(n) - each node is processed once via parallel stream - 时间复杂度: O(n) - 通过并行流每个节点仅处理一次
  • Space complexity: O(n) - ConcurrentHashMap and roots list proportional to node count - 空间复杂度: O(n) - ConcurrentHashMap 和根节点列表与节点数成正比
Since:
JDK 25, opencode-base-tree V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • build

      public static <T extends Treeable<T,ID>, ID> List<T> build(List<T> nodes)
      Build tree concurrently 并发构建树
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      nodes - the node list | 节点列表
      Returns:
      the root nodes | 根节点列表
    • build

      public static <T extends Treeable<T,ID>, ID> List<T> build(List<T> nodes, ID rootId)
      Build tree concurrently with root ID 使用根ID并发构建树
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      nodes - the node list | 节点列表
      rootId - the root ID | 根ID
      Returns:
      the root nodes | 根节点列表
    • buildLarge

      public static <T extends Treeable<T,ID>, ID> List<T> buildLarge(List<T> nodes, ID rootId, int parallelThreshold)
      Build large tree with parallel processing 使用并行处理构建大型树
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      nodes - the node list | 节点列表
      rootId - the root ID | 根ID
      parallelThreshold - the threshold for parallel processing | 并行处理阈值
      Returns:
      the root nodes | 根节点列表