1、将整棵树状结构对象分解成可直接读取节点的普通对象列表
2、将仅有当前节点和父节点的对象列表整合成树状结构数据对象
3、根据第一点直接读取指定节点的详细数据信息
创建节点抽象类,用户可自定义类继承此抽象类,数据转换时使用
package entity;
/**
* 平铺数据结构方法
*/
public interface TreeSourceNode {
// 获取节点ID
public abstract String getId();
// 获取节点名称
public abstract String getName();
// 获取父节点ID
public abstract String getParentId();
}
package entity;
import java.util.List;
/**
* 树形结构节点
*/
public class TreeStructure {
//唯一标识
private String id;
//名称
private String name;
//父节点
private String parentId;
//级别
private int level;
//子节点
private List<TreeStructure> children;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public List<TreeStructure> getChildren() {
return children;
}
public void setChildren(List<TreeStructure> children) {
this.children = children;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
package example;
import entity.TreeSourceNode;
/**
* @Description: 机型节点实体
* @Author: ld
* @Date: 2021-01-16 16:17
*/
public class ActTypeNodeVo implements TreeSourceNode {
private String parentId;
private String id;
private String name;
private String level;
@Override
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
@Override
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
}
package utile;
import entity.TreeStructure;
import entity.TreeTileNode;
import java.util.ArrayList;
import java.util.List;
/**
* @Description: 树状结构工具类
* @Author: ld
* @Date: 2021-01-28 17:55
*/
public class TreeHelper {
private TreeHelper(){
}
/**
* 获取指定树状结构中的某节点的详细数据
* @param treeStructureList 指定树状结构列表数据
* @param nodeId 指定节点id
* @return 指定id的详细数据
*/
public static TreeStructure getNode(List<TreeStructure> treeStructureList, String nodeId) {
for (TreeStructure obj: convertToTileList(treeStructureList)) {
if(nodeId.equals(obj.getId())){
return obj;
}
}
return null;
}
/**
* 将树状结构转换为平铺数据结构(将含有children的树状结构转换为仅有节点和父节点的对象列表)
* @param source 树状结构数据列表
* @return 平铺数据结构列表
*/
public static List<TreeStructure> convertToTileList(List<TreeStructure> source){
List<TreeStructure> outList = new ArrayList<>();
for (TreeStructure tTreeStructure : source) {
if (tTreeStructure.getChildren() != null) {
List<TreeStructure> treeStructureList = tTreeStructure.getChildren();
outList.add(tTreeStructure);
convertChildrenToTileList(treeStructureList, outList);
//若不需要字节点,则可设置为空
// tTreeStructure.setChildren(null);
} else {
outList.add(tTreeStructure);
}
}
return outList;
}
/**
* 将树状结构转换为平铺数据结构(将含有children的树状结构转换为仅有节点和父节点的对象列表)
* @param source 树状结构数据列表
* @param outList 平铺数据结构列表
*/
private static void convertChildrenToTileList(List<TreeStructure> source , List<TreeStructure> outList){
if(outList==null){
outList = new ArrayList<>();
}
for (TreeStructure tTreeStructure : source) {
if (tTreeStructure.getChildren() != null) {
List<TreeStructure> treeStructureList = tTreeStructure.getChildren();
outList.add(tTreeStructure);
convertChildrenToTileList(treeStructureList, outList);
//若不需要字节点,则可设置为空
// tTreeStructure.setChildren(null);
} else {
outList.add(tTreeStructure);
}
}
}
/**
* 平铺数据建立成树(将仅有节点和父节点的对象列表转换为含有children的树状结构)
* @param tileStructureList 平铺数据列表
* @param 任意
* @return 树状结构数据
*/
public static <T extends TreeTileNode> List<TreeStructure> convertToTreeList(List<T> tileStructureList) {
List<TreeStructure> treeStructureList = new ArrayList<>();
List<String> allIds = new ArrayList<>();
for (T sourceNode : tileStructureList) {
allIds.add(sourceNode.getId());
}
for (T tileNode : tileStructureList) {
if (!allIds.contains(tileNode.getParentId())) {
TreeStructure treeStructure = new TreeStructure();
treeStructure.setId(tileNode.getId());
treeStructure.setName(tileNode.getName());
//第一层设置等级为1
treeStructure.setLevel(1);
treeStructure.setParentId(tileNode.getParentId());
List<TreeStructure> children = convertChildrenToTreeList(tileStructureList, treeStructure);
treeStructure.setChildren(children.isEmpty() ? null : children);
treeStructureList.add(treeStructure);
}
}
return treeStructureList;
}
private static <T extends TreeTileNode> List<TreeStructure> convertChildrenToTreeList(List<T> tileStructureList, TreeStructure node) {
List<TreeStructure> childrenList = new ArrayList<>();
for (T tileNode : tileStructureList) {
if (node.getId().equals(tileNode.getParentId())) {
TreeStructure treeStructure = new TreeStructure();
treeStructure.setId(tileNode.getId());
treeStructure.setName(tileNode.getName());
treeStructure.setLevel(node.getLevel() + 1);
treeStructure.setParentId(tileNode.getParentId());
List<TreeStructure> children = convertChildrenToTreeList(tileStructureList, treeStructure);
treeStructure.setChildren(children.isEmpty() ? null : children);
childrenList.add(treeStructure);
}
}
return childrenList;
}
}
将上边的树状结构图转换成平铺对象列表
若不需要子节点children,可以在代码中设置setChildren(null)
自己调用下方法试试