Pid and id are in the same table, traverse to obtain tree nodes
Results of the
Define keyvalue object
@Data
public class keyvalue {
public keyvalue(String pid, String id) {
this.pid = pid;
this.id = id;
}
public String pid;
public String id;
}
Define tree structure object
@Data
public class ProjectTree {
private String id;
private String name;
private String childrenId;
private List<ProjectTree> children;
}
simulated data
public List<keyvalue> keyvalueList()
{
List<keyvalue> keyvalues = new ArrayList<>();
keyvalues.add(new keyvalue("", "1001596232348864541"));
keyvalues.add(new keyvalue("", "1001596232348864542"));
keyvalues.add(new keyvalue("", "1001596232348864543"));
keyvalues.add(new keyvalue("1001596232348864513", "1001596232348864521"));
keyvalues.add(new keyvalue("1001596232348864514", "1001596232348864522"));
keyvalues.add(new keyvalue("1001596232348864515", "1001596232348864533"));
keyvalues.add(new keyvalue("1001596232348864516", "1001596232348864534"));
keyvalues.add(new keyvalue("1001596232348864517", "1001596232348864535"));
keyvalues.add(new keyvalue("1001596232348864518", "1001596232348864513"));
keyvalues.add(new keyvalue("1001596232348864519", "1001596232348864514"));
keyvalues.add(new keyvalue("1001596232348864520", "1001596232348864515"));
keyvalues.add(new keyvalue("1001596232348864521", "1001596232348864536"));
keyvalues.add(new keyvalue("1001596232348864522", "1001596232348864537"));
keyvalues.add(new keyvalue("1001596232348864533", "1001596232348864538"));
keyvalues.add(new keyvalue("1001596232348864534", "1001596232348864539"));
keyvalues.add(new keyvalue("1001596232348864535", "1001596232348864540"));
keyvalues.add(new keyvalue("1001596232348864536", "1001596232348864546"));
keyvalues.add(new keyvalue("1001596232348864537", "1001596232348864547"));
keyvalues.add(new keyvalue("1001596232348864538", "1001596232348864548"));
keyvalues.add(new keyvalue("1001596232348864539", "1001596232348864549"));
keyvalues.add(new keyvalue("1001596232348864540", "1001596232348864550"));
keyvalues.add(new keyvalue("1001596232348864541", "1001596232348864518"));
keyvalues.add(new keyvalue("1001596232348864542", "1001596232348864519"));
keyvalues.add(new keyvalue("1001596232348864543", "1001596232348864520"));
return keyvalues;
}
Traverse the first level menu
private static List<ProjectTree> getTree(List<ProjectTree> rootList, List<ProjectTree> bodyList) {
if (bodyList != null && !bodyList.isEmpty()) {
Map<String, String> map = new Hashtable<>(bodyList.size());
rootList.forEach(rootTree -> getChild(rootTree, map, bodyList));
return rootList;
} else if (rootList != null) {
return rootList;
}
return null;
}
Traverse sub-objects
private static void getChild(ProjectTree treeDto, Map<String, String> map, List<ProjectTree> bodyList) {
List<ProjectTree> childList = new ArrayList();
bodyList.stream()
.filter(c -> !map.containsKey(c.getId()))
.filter(c -> c.getChildrenId().equals(treeDto.getId()))
.forEach(c -> {
map.put(c.getId(), c.getChildrenId());
getChild(c, map, bodyList);
childList.add(c);
});
treeDto.setChildren(childList);
}
start up
public static List<ProjectTree> startTest() {
List<ProjectTree> rootList = new ArrayList<>();
List<ProjectTree> bodyList = new ArrayList<>();
for (keyvalue keyvalue : keyvalueList()) {
if (keyvalue.getPid().isEmpty()) {
ProjectTree pj = new ProjectTree();
pj.setId(keyvalue.id);
pj.setName("一级目录");
pj.setChildrenId("");
rootList.add(pj);
} else {
ProjectTree pj = new ProjectTree();
pj.setId(keyvalue.id);
pj.setName("子集目录");
pj.setChildrenId(keyvalue.pid);
bodyList.add(pj);
}
}
List<ProjectTree> stc = getTree(rootList, bodyList);
return stc;
}