代码更新
This commit is contained in:
@ -171,27 +171,42 @@ public class ExcelReader {
|
||||
Map<String, TreeNode> nodeMap = new HashMap<>();
|
||||
nodeMap.put("", root);
|
||||
List<String> nonEmptyKeys = new ArrayList<>(); // 记录非空A列值的顺序
|
||||
Map<String, String> lastChineseNumberMap = new HashMap<>(); // 记录各级别最近的中文数字节点
|
||||
|
||||
// 初始化根级中文数字节点
|
||||
lastChineseNumberMap.put("", "");
|
||||
|
||||
for (List<String> row : data) {
|
||||
String key = row.get(0).trim();
|
||||
TreeNode node = new TreeNode(row);
|
||||
|
||||
if (!key.isEmpty()) {
|
||||
String parentKey = getParentKey(key);
|
||||
String parentKey = getParentKey(key, lastChineseNumberMap, nonEmptyKeys);
|
||||
|
||||
// 更新最近的中文数字节点记录
|
||||
if (key.matches(CHINESE_NUMBERS_REGEX)) {
|
||||
String currentLevelParentKey = getParentKey(key, lastChineseNumberMap, nonEmptyKeys);
|
||||
lastChineseNumberMap.put(key, key);
|
||||
lastChineseNumberMap.put(parentKey + ".children", key);
|
||||
}
|
||||
|
||||
TreeNode parent = nodeMap.get(parentKey);
|
||||
|
||||
if (parent == null) {
|
||||
parent = new TreeNode(new ArrayList<>());
|
||||
nodeMap.put(parentKey, parent);
|
||||
TreeNode grandParent = nodeMap.get(getParentKey(parentKey));
|
||||
TreeNode grandParent = nodeMap.get(getParentKey(parentKey, lastChineseNumberMap, nonEmptyKeys));
|
||||
if (grandParent != null) {
|
||||
parent.setParent(grandParent);
|
||||
grandParent.addChild(parent);
|
||||
} else {
|
||||
// 如果没有更高级别的父节点,就挂到根节点
|
||||
parent.setParent(root);
|
||||
root.addChild(parent);
|
||||
}
|
||||
}
|
||||
|
||||
node.setParent(parent);
|
||||
parent.addChild(node);
|
||||
nodeMap.put(key, node);
|
||||
nonEmptyKeys.add(key);
|
||||
@ -201,9 +216,11 @@ public class ExcelReader {
|
||||
String lastKey = nonEmptyKeys.get(nonEmptyKeys.size() - 1);
|
||||
TreeNode parent = nodeMap.get(lastKey);
|
||||
if (parent != null) {
|
||||
node.setParent(parent);
|
||||
parent.addChild(node);
|
||||
}
|
||||
} else {
|
||||
node.setParent(root);
|
||||
root.addChild(node);
|
||||
}
|
||||
}
|
||||
@ -211,6 +228,51 @@ public class ExcelReader {
|
||||
|
||||
return root;
|
||||
}
|
||||
// public static TreeNode buildTree(List<List<String>> data) {
|
||||
// TreeNode root = new TreeNode(new ArrayList<>());
|
||||
// Map<String, TreeNode> nodeMap = new HashMap<>();
|
||||
// nodeMap.put("", root);
|
||||
// List<String> nonEmptyKeys = new ArrayList<>(); // 记录非空A列值的顺序
|
||||
//
|
||||
// for (List<String> row : data) {
|
||||
// String key = row.get(0).trim();
|
||||
// TreeNode node = new TreeNode(row);
|
||||
//
|
||||
// if (!key.isEmpty()) {
|
||||
// String parentKey = getParentKey(key);
|
||||
// TreeNode parent = nodeMap.get(parentKey);
|
||||
//
|
||||
// if (parent == null) {
|
||||
// parent = new TreeNode(new ArrayList<>());
|
||||
// nodeMap.put(parentKey, parent);
|
||||
// TreeNode grandParent = nodeMap.get(getParentKey(parentKey));
|
||||
// if (grandParent != null) {
|
||||
// grandParent.addChild(parent);
|
||||
// } else {
|
||||
// // 如果没有更高级别的父节点,就挂到根节点
|
||||
// root.addChild(parent);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// parent.addChild(node);
|
||||
// nodeMap.put(key, node);
|
||||
// nonEmptyKeys.add(key);
|
||||
// } else {
|
||||
// // A列为空,挂到上一个非空节点下
|
||||
// if (!nonEmptyKeys.isEmpty()) {
|
||||
// String lastKey = nonEmptyKeys.get(nonEmptyKeys.size() - 1);
|
||||
// TreeNode parent = nodeMap.get(lastKey);
|
||||
// if (parent != null) {
|
||||
// parent.addChild(node);
|
||||
// }
|
||||
// } else {
|
||||
// root.addChild(node);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return root;
|
||||
// }
|
||||
|
||||
/**
|
||||
* 以可视化方式打印树形结构
|
||||
@ -270,7 +332,7 @@ public class ExcelReader {
|
||||
/**
|
||||
* 根据A列的值确定父节点的键
|
||||
*/
|
||||
private static String getParentKey(String key) {
|
||||
private static String getParentKey(String key, Map<String, String> lastChineseNumberMap, List<String> nonEmptyKeys) {
|
||||
// 中文数字(一、二、三...)的父节点是根节点
|
||||
if (key.matches(CHINESE_NUMBERS_REGEX)) {
|
||||
return "";
|
||||
@ -282,14 +344,44 @@ public class ExcelReader {
|
||||
if (lastDotIndex > 0) {
|
||||
return key.substring(0, lastDotIndex);
|
||||
} else {
|
||||
// 一级数字(如1、2)的父节点是根节点或最近的中文数字节点
|
||||
return "";
|
||||
// 一级数字(如1、2)的父节点是最近的中文数字节点
|
||||
if (!nonEmptyKeys.isEmpty()) {
|
||||
// 查找最近的中文数字节点作为父节点
|
||||
for (int i = nonEmptyKeys.size() - 1; i >= 0; i--) {
|
||||
String prevKey = nonEmptyKeys.get(i);
|
||||
if (prevKey.matches(CHINESE_NUMBERS_REGEX)) {
|
||||
return prevKey;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 如果没有找到中文数字节点,使用根节点
|
||||
return lastChineseNumberMap.getOrDefault("", "");
|
||||
}
|
||||
}
|
||||
|
||||
// 其他格式默认父节点是根节点
|
||||
return "";
|
||||
}
|
||||
// private static String getParentKey(String key) {
|
||||
// // 中文数字(一、二、三...)的父节点是根节点
|
||||
// if (key.matches(CHINESE_NUMBERS_REGEX)) {
|
||||
// return "";
|
||||
// }
|
||||
//
|
||||
// // 数字格式(1、1.1、1.2.1等)的父节点是上一级
|
||||
// if (key.matches("\\d+(\\.\\d+)*")) {
|
||||
// int lastDotIndex = key.lastIndexOf('.');
|
||||
// if (lastDotIndex > 0) {
|
||||
// return key.substring(0, lastDotIndex);
|
||||
// } else {
|
||||
// // 一级数字(如1、2)的父节点是根节点或最近的中文数字节点
|
||||
// return "";
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // 其他格式默认父节点是根节点
|
||||
// return "";
|
||||
// }
|
||||
|
||||
/**
|
||||
* Excel数据实体类,包含所有工作表数据
|
||||
@ -333,13 +425,18 @@ public class ExcelReader {
|
||||
/**
|
||||
* 树形节点类
|
||||
*/
|
||||
/**
|
||||
* 树形节点类,增加了父节点引用
|
||||
*/
|
||||
public static class TreeNode {
|
||||
List<String> data;
|
||||
List<TreeNode> children;
|
||||
TreeNode parent; // 新增:父节点引用
|
||||
|
||||
public TreeNode(List<String> data) {
|
||||
this.data = data;
|
||||
this.children = new ArrayList<>();
|
||||
this.parent = null;
|
||||
}
|
||||
|
||||
public void addChild(TreeNode child) {
|
||||
@ -353,6 +450,35 @@ public class ExcelReader {
|
||||
public List<TreeNode> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public TreeNode getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(TreeNode parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
}
|
||||
// public static class TreeNode {
|
||||
// List<String> data;
|
||||
// List<TreeNode> children;
|
||||
//
|
||||
// public TreeNode(List<String> data) {
|
||||
// this.data = data;
|
||||
// this.children = new ArrayList<>();
|
||||
// }
|
||||
//
|
||||
// public void addChild(TreeNode child) {
|
||||
// this.children.add(child);
|
||||
// }
|
||||
//
|
||||
// public List<String> getData() {
|
||||
// return data;
|
||||
// }
|
||||
//
|
||||
// public List<TreeNode> getChildren() {
|
||||
// return children;
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user