如何在Java中按树形结构分组点分隔字符串为指定格式?
问题需求
给定一组表示树形结构的点分隔字符串数组:
String[] paths = { "323-060", "323-060.040", "323-060.040.030", "323-060.088", "323-060.088.020", "323-060.010", "323-060.010.080", "323-060.010.080.020", "323-060.010.080.060" };
该数组对应树形结构:
323-060 /*root*/ 040 /*second level, full path: 323-060.040*/ 030 /* third level, full path: 323-060.040.030 */ 088 020 010 080 020 060
需要将其转换为以下格式的字符串列表(每个字符串表示从根到叶子节点的完整路径,用反斜杠\分隔):
"323-060\323-060.040\323-060.040.030" "323-060\323-060.088\323-060.088.020" "323-060\323-060.010\323-060.010.080\323-060.010.080.020" "323-060\323-060.010\323-060.010.080\323-060.010.080.060"
Java实现方案
实现思路
- 构建树形结构:拆分每个输入路径的分段,建立节点间的父子关系,每个节点存储自身完整路径和子节点列表。
- 遍历叶子路径:从根节点出发递归遍历所有分支,遇到无后代的叶子节点时,拼接从根到该节点的所有路径,收集到结果列表。
代码实现
1. 定义节点类
用于存储节点的完整路径和子节点集合:
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; class Node { private String fullPath; private Map<String, Node> children; public Node(String fullPath) { this.fullPath = fullPath; this.children = new HashMap<>(); } public String getFullPath() { return fullPath; } public Map<String, Node> getChildren() { return children; } public boolean isLeaf() { return children.isEmpty(); } }
2. 核心逻辑实现
完成树的构建和叶子路径的收集:
public class TreePathConverter { public static void main(String[] args) { String[] inputPaths = { "323-060", "323-060.040", "323-060.040.030", "323-060.088", "323-060.088.020", "323-060.010", "323-060.010.080", "323-060.010.080.020", "323-060.010.080.060" }; List<String> result = convertPaths(inputPaths); // 打印目标格式结果 for (String path : result) { System.out.println("\"" + path + "\""); } } public static List<String> convertPaths(String[] inputPaths) { Node root = buildTree(inputPaths); List<String> result = new ArrayList<>(); collectLeafPaths(root, new ArrayList<>(), result); return result; } private static Node buildTree(String[] inputPaths) { Node root = null; for (String path : inputPaths) { String[] segments = path.split("\\."); Node current = null; for (int i = 0; i < segments.length; i++) { String fullPath = String.join(".", java.util.Arrays.copyOfRange(segments, 0, i + 1)); if (i == 0) { if (root == null) { root = new Node(fullPath); } current = root; } else { if (!current.getChildren().containsKey(fullPath)) { current.getChildren().put(fullPath, new Node(fullPath)); } current = current.getChildren().get(fullPath); } } } return root; } private static void collectLeafPaths(Node currentNode, List<String> currentPath, List<String> result) { currentPath.add(currentNode.getFullPath()); if (currentNode.isLeaf()) { String joinedPath = String.join("\\", currentPath); result.add(joinedPath); } else { for (Node child : currentNode.getChildren().values()) { collectLeafPaths(child, new ArrayList<>(currentPath), result); } } } }
代码说明
- buildTree方法:遍历所有输入路径,按
.拆分后逐个创建节点,维护节点间的父子关联,确保每个节点的完整路径被正确存储。 - collectLeafPaths方法:递归遍历树结构,将当前节点路径加入临时列表;遇到叶子节点时,用
\拼接临时列表中的所有路径,存入结果集合。 - 运行代码后,输出结果与需求完全匹配。
内容的提问来源于stack exchange,提问作者Evgeniy Skiba
相关产品推荐
相关产品推荐

