我有以下路径列表:
private static final List<String> paths = Arrays.asList(
"assets/css/custom.css",
"assets/css/default.css",
"assets/js/main.js",
"assets/js/old/main-old.js",
"fonts/poppins.woff",
"favicon.ico",
"index.html"
);
我需要创建一个可搜索的树,像这样:
这就是我现在拥有的:
public void testCreateTree() {
Node root = new Node("ROOT", null, Node.NODE_TYPE.ROOT);
paths.forEach(path -> {
final Node[] currentNode = {root};
if(!path.contains("/")) { // root files
currentNode[0].addChild(new Node(path, currentNode[0], Node.NODE_TYPE.FILE));
} else {
String folders = DirectoryRegex.matchFolders(path); // e.g. matches/returns "root/"
String fileName = DirectoryRegex.matchFile(path); // e.g. matches/returns index.html
String[] folderArrays = folders.split("/");
Arrays.asList(folderArrays).forEach(folder -> {
Node node = new Node("ROOT", null, Node.NODE_TYPE.ROOT);
node.setNodeName(folder);
node.setNodeType(Node.NODE_TYPE.FOLDER);
node.setParent(currentNode[0]);
// check if child exists
Node existingNode = currentNode[0].getChild(folder, Node.NODE_TYPE.FOLDER);
if(existingNode == null) {
existingNode = node;
currentNode[0].addChild(node);
}
currentNode[0] = existingNode;
});
currentNode[0].addChild(new Node(fileName, currentNode[0], Node.NODE_TYPE.FILE));
}
});
String print = root.printNodeJSON().toString();
Console.log(print);
}
Node.java 类是这样的:
public class Node {
public NODE_TYPE getNodeType() {
return nodeType;
}
public void setNodeType(NODE_TYPE nodeType) {
this.nodeType = nodeType;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public List<Node> getChildren() {
if(children == null) {
children = new LinkedList<>();
}
return children;
}
public void setChildren(List<Node> children) {
this.children = children;
}
public void addChild(Node child) {
getChildren().add(child);
}
public Node getChild(String nodeName, NODE_TYPE nodeType) {
final Node[] child = {null};
getChildren().forEach(node -> {
if(node.getNodeName().equals(nodeName) && node.getNodeType().equals(nodeType)) {
child[0] = node;
}
});
return child[0];
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
private Node() {}
public Node(String nodeName, Node parent, NODE_TYPE nodeType) {
setNodeName(nodeName);
setNodeType(nodeType);
setParent(parent);
}
public enum NODE_TYPE { FILE, FOLDER, ROOT }
private NODE_TYPE nodeType;
private Node parent;
private List<Node> children;
private String nodeName;
public String printNode() {
final String[] s = {"["};
s[0] = s[0] + "Node name: " + nodeName + ",";
if(nodeType != null) {
s[0] = s[0] + "Node type: " + nodeType.toString() + ",";
}
if(getParent() != null) {
s[0] = s[0] + "Node Parent: [ name = " + getParent().getNodeName() + ", type = " + getParent().getNodeType() + " ]";
}
s[0] = s[0] + "Node children: [";
getChildren().forEach(child -> {
s[0] = "[" + s[0] + child.printNode() + "]";
});
s[0] = s[0] + "]";
s[0] = s[0] + "]";
return s[0];
}
public JSONObject printNodeJSON() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("nodeName", nodeName);
jsonObject.put("nodeType", nodeType != null ? nodeType.toString() : null);
jsonObject.put("parent", getParent() != null ? getParent().printNodeJSONWithoutChildren() : null);
JSONArray children = new JSONArray();
getChildren().forEach(child -> {
children.put(child.printNodeJSON());
});
jsonObject.put("children", children);
return jsonObject;
}
public JSONObject printNodeJSONWithoutChildren() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("nodeName", nodeName);
jsonObject.put("nodeType", nodeType != null ? nodeType.toString() : null);
jsonObject.put("parent", getParent() != null ? getParent().printNodeJSONWithoutChildren() : null);
// JSONArray children = new JSONArray();
// getChildren().forEach(child -> {
// children.put(child.printNodeJSON());
// });
// jsonObject.put("children", children);
return jsonObject;
}
}
代码工作正常,但我想知道执行此操作的最有效方法。