方法
public interface TreeNode {
static <E extends com.domain.TreeNode> List<E> toTreeList(@NonNull Collection<E> source) {
if (source.size() < 2) {
return new ArrayList<>(source);
}
List<E> result= new ArrayList<>();
Map<Object, E> sourceMap = new HashMap<>();
for (E e : source){
if (e != null) {
Object nodeId = e.getNodeId();
sourceMap.put(nodeId, e);
}
}
for (E e : source){
Object parentNodeId = e.getParentNodeId();
E node = null;
if (parentNodeId != null){
node = sourceMap.get(parentNodeId);
}
if (node == null){
result.add(e);
}else{
List childNodes = node.getChildNodes();
childNodes.add(e);
}
}
return result;
}
}
具体实现
/**
* 对象List转为Tree树形结构
* @param entityList 传进来的泛型List
* @param parentFieldName 父级字段名称
* @return
*/
public static List<Map<String, Object>> listToTree(List<T> entityList, String parentFieldName){
//返回的map Tree树形结构
List<Map<String, Object>> treeMap = new ArrayList<>();
//将传进的参数entityList转为MapList
List<Map<String, Object>> listMap = JSON.parseObject(JSON.toJSONString(entityList), List.class);
//声明一个map用来存listMap中的对象,key为对象id,value为对象本身
Map<String, Map<String, Object>> entityMap = new Hashtable<>();
//循环listMap把map对象put到entityMap中去
listMap.forEach(map -> entityMap.put(map.get("id").toString(), map));
//循环listMap进行Tree树形结构组装
listMap.forEach(map -> {
//获取map的pid
Object pid = map.get(parentFieldName);
if (pid == null){ //判断pid是否为空,为空说明是最顶级,直接add到返回的treeMap中去
treeMap.add(map);
} else { //如果pid不为空
//根据当前map的pid获取上级 parentMap
Map<String, Object> parentMap = entityMap.get(pid);
if (parentMap == null){ //如果parentMap为空,则说明当前map没有父级,当前map就是顶级
treeMap.add(map);
} else { //如果parentMap不为空,则当前map为parentMap的子级
//取出parentMap的所有子级的List集合
List<Map<String, Object>> children = (List<Map<String, Object>>)parentMap.get("children");
if (children == null){ //判断子级集合是否为空,为空则新创建List
children = new ArrayList<>();
parentMap.put("children", children);
}
//把当前map对象add到parentMap的子级List中去
children.add(map);
/**
* 因为parentMap是从entityMap中get出来的,
* 而entityMap中的value又是来自于listMap对象,
* 所以parentMap和entityMap中的value的地址都是指向listMap中的对象,
* 所以parentMap的children和entityMap中的value的children改变时,都会改变listMap中的对象。
*/
}
}
});
return treeMap;
}
总结
因为parentMap是从entityMap中get出来的,而entityMap中的value又是来自于listMap对象,所以parentMap和entityMap中的value的地址都是指向listMap中的对象,所以parentMap的children和entityMap中的value的children改变时,都会改变listMap中的对象。