Class OpenTree
java.lang.Object
cloud.opencode.base.tree.OpenTree
Open Tree
开放树
Main facade for tree operations.
树操作的主要门面。
Features | 主要功能:
- Build trees from flat lists - 从扁平列表构建树
- Multiple traversal strategies (pre-order, post-order, BFS) - 多种遍历策略(前序、后序、广度优先)
- Search and filter operations - 搜索和过滤操作
- Tree statistics (depth, size, leaves) - 树统计(深度、大小、叶子节点)
- Virtual tree with lazy loading - 支持懒加载的虚拟化树
- Serialization to JSON, XML, Map - 序列化为JSON、XML、Map
Usage Examples | 使用示例:
// Build tree from flat list - 从扁平列表构建树
List<MyNode> tree = OpenTree.buildTree(flatList);
// Find node by ID - 通过ID查找节点
MyNode node = OpenTree.find(tree, nodeId);
// Serialize to JSON - 序列化为JSON
String json = OpenTree.toJson(tree);
Security | 安全性:
- Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
- Null-safe: Partial (null checks on input lists) - 空值安全: 部分(对输入列表有空值检查)
- Since:
- JDK 25, opencode-base-tree V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionbuild(Collection<T> items, Function<T, ID> idExtractor, Function<T, ID> parentIdExtractor) static <T,ID> TreeNode <T> buildSingle(Collection<T> items, Function<T, ID> idExtractor, Function<T, ID> parentIdExtractor) Build tree from flat list (default root detection) 从扁平列表构建树(默认根节点检测)Build tree from flat list with specified root ID 从扁平列表构建树(指定根节点ID)static List<DefaultTreeNode<Object>> Build tree from maps 从Map列表构建树buildTreeSorted(List<T> nodes, ID rootId, Comparator<T> comparator) Build sorted tree 构建排序树static <T extends Treeable<T,?>>
intCalculate tree depth 计算树深度Filter tree (keep matching nodes and their ancestors) 过滤树(保留匹配节点及其祖先)static <T extends Treeable<T,ID>, ID>
TFind node by ID 按ID查找节点Find all nodes matching predicate 查找所有满足条件的节点static <T> List<T> flattenTree(List<T> roots) Flatten tree to list 将树扁平化为列表static <T> List<TreeBuilder.NodeWithDepth<T>> flattenWithDepth(TreeNode<T> root) Get all leaf nodes 获取所有叶子节点Get path from root to specified node 获取从根到指定节点的路径static <T> TreeNode<T> node(T data) static <T,ID> void preloadVirtualTree(VirtualTree<T, ID> root, int depth) Preload virtual tree to specified depth 预加载虚拟树到指定深度static <T> Stringstatic <T> voidprintToConsole(TreeNode<T> root) static <T extends Treeable<T,?>>
intCalculate total node count 计算节点总数static <T extends Treeable<T,?>>
voidsortTree(List<T> nodes, Comparator<T> comparator) Sort tree recursively 递归排序树toFlatMaps(List<T> roots) Convert tree to flat list of maps (without hierarchy) 将树扁平化为Map列表(无层级)Serialize tree to JSON 将树序列化为JSONSerialize tree to JSON with custom field extractor 使用自定义字段提取器将树序列化为JSONConvert tree to list of maps 将树转换为Map列表Serialize tree to XML 将树序列化为XMLSerialize tree to XML with custom field extractor 使用自定义字段提取器将树序列化为XMLstatic <T extends Treeable<T,?>>
voidtraverseBreadthFirst(List<T> roots, Consumer<T> visitor) Breadth-first (level order) traversal 广度优先(层序)遍历static <T extends Treeable<T,?>>
voidtraversePostOrder(List<T> roots, Consumer<T> visitor) Post-order traversal 后序遍历static <T extends Treeable<T,?>>
voidtraversePreOrder(List<T> roots, Consumer<T> visitor) Pre-order traversal 前序遍历static <T extends Treeable<T,?>>
voidtraverseWithDepth(List<T> roots, BiConsumer<T, Integer> visitor) Traverse with depth information 带深度信息的遍历static <T> StringSerialize TreeNode to JSON 将TreeNode序列化为JSONstatic <T> StringSerialize TreeNode to XML 将TreeNode序列化为XMLstatic <T,ID> VirtualTree <T, ID> virtualTree(ID id, T data, LazyChildLoader<VirtualTree<T, ID>, ID> childLoader) Create a virtual tree root with lazy loading 创建支持懒加载的虚拟树根节点static <T,ID> VirtualTree.Builder <T, ID> Create a virtual tree builder 创建虚拟树构建器
-
Method Details
-
node
-
build
public static <T,ID> List<TreeNode<T>> build(Collection<T> items, Function<T, ID> idExtractor, Function<T, ID> parentIdExtractor) -
buildSingle
public static <T,ID> TreeNode<T> buildSingle(Collection<T> items, Function<T, ID> idExtractor, Function<T, ID> parentIdExtractor) -
fromMap
-
flatten
-
flattenWithDepth
-
print
-
printToConsole
-
buildTree
-
buildTree
Build tree from flat list with specified root ID 从扁平列表构建树(指定根节点ID)- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
nodes- the flat list | 扁平列表rootId- the root ID | 根节点ID- Returns:
- the root nodes | 根节点列表
-
buildTreeSorted
public static <T extends Treeable<T,ID>, ID> List<T> buildTreeSorted(List<T> nodes, ID rootId, Comparator<T> comparator) Build sorted tree 构建排序树- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
nodes- the flat list | 扁平列表rootId- the root ID | 根节点IDcomparator- the comparator | 比较器- Returns:
- the sorted root nodes | 排序后的根节点列表
-
buildTreeFromMaps
public static List<DefaultTreeNode<Object>> buildTreeFromMaps(List<Map<String, Object>> maps, String idKey, String parentIdKey, String nameKey) Build tree from maps 从Map列表构建树- Parameters:
maps- the map list | Map列表idKey- the ID key | ID键parentIdKey- the parent ID key | 父ID键nameKey- the name key | 名称键- Returns:
- the root nodes | 根节点列表
-
traversePreOrder
-
traversePostOrder
-
traverseBreadthFirst
-
traverseWithDepth
public static <T extends Treeable<T,?>> void traverseWithDepth(List<T> roots, BiConsumer<T, Integer> visitor) Traverse with depth information 带深度信息的遍历- Type Parameters:
T- the node type | 节点类型- Parameters:
roots- the root nodes | 根节点列表visitor- the visitor | 访问者
-
find
-
findAll
-
getPath
Get path from root to specified node 获取从根到指定节点的路径- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
roots- the root nodes | 根节点列表id- the target node ID | 目标节点ID- Returns:
- the path from root to node | 从根到节点的路径
-
getLeaves
-
filter
Filter tree (keep matching nodes and their ancestors) 过滤树(保留匹配节点及其祖先)- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
roots- the root nodes | 根节点列表predicate- the predicate | 谓词- Returns:
- the filtered root nodes | 过滤后的根节点列表
-
flattenTree
-
depth
-
size
-
sortTree
Sort tree recursively 递归排序树- Type Parameters:
T- the node type | 节点类型- Parameters:
nodes- the nodes to sort | 要排序的节点comparator- the comparator | 比较器
-
virtualTree
public static <T,ID> VirtualTree<T,ID> virtualTree(ID id, T data, LazyChildLoader<VirtualTree<T, ID>, ID> childLoader) Create a virtual tree root with lazy loading 创建支持懒加载的虚拟树根节点- Type Parameters:
T- the data type | 数据类型ID- the ID type | ID类型- Parameters:
id- the root ID | 根节点IDdata- the root data | 根节点数据childLoader- the lazy child loader | 懒加载器- Returns:
- the virtual tree root | 虚拟树根节点
-
virtualTreeBuilder
Create a virtual tree builder 创建虚拟树构建器- Type Parameters:
T- the data type | 数据类型ID- the ID type | ID类型- Returns:
- the builder | 构建器
-
preloadVirtualTree
Preload virtual tree to specified depth 预加载虚拟树到指定深度- Type Parameters:
T- the data type | 数据类型ID- the ID type | ID类型- Parameters:
root- the virtual tree root | 虚拟树根节点depth- the depth to preload | 预加载深度
-
toJson
-
toJson
public static <T extends Treeable<T,ID>, ID> String toJson(List<T> roots, Function<T, Map<String, Object>> fieldExtractor) Serialize tree to JSON with custom field extractor 使用自定义字段提取器将树序列化为JSON- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
roots- the root nodes | 根节点列表fieldExtractor- the field extractor | 字段提取器- Returns:
- the JSON string | JSON字符串
-
toXml
-
toXml
public static <T extends Treeable<T,ID>, ID> String toXml(List<T> roots, Function<T, Map<String, Object>> fieldExtractor) Serialize tree to XML with custom field extractor 使用自定义字段提取器将树序列化为XML- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
roots- the root nodes | 根节点列表fieldExtractor- the field extractor | 字段提取器- Returns:
- the XML string | XML字符串
-
toMaps
-
toFlatMaps
Convert tree to flat list of maps (without hierarchy) 将树扁平化为Map列表(无层级)- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
roots- the root nodes | 根节点列表- Returns:
- the flat map list | 扁平Map列表
-
treeNodeToJson
public static <T> String treeNodeToJson(TreeNode<T> root, Function<T, Map<String, Object>> dataSerializer) Serialize TreeNode to JSON 将TreeNode序列化为JSON- Type Parameters:
T- the data type | 数据类型- Parameters:
root- the root node | 根节点dataSerializer- function to serialize data | 数据序列化函数- Returns:
- the JSON string | JSON字符串
-
treeNodeToXml
public static <T> String treeNodeToXml(TreeNode<T> root, Function<T, Map<String, Object>> dataSerializer) Serialize TreeNode to XML 将TreeNode序列化为XML- Type Parameters:
T- the data type | 数据类型- Parameters:
root- the root node | 根节点dataSerializer- function to serialize data | 数据序列化函数- Returns:
- the XML string | XML字符串
-