Java树形结构工具类

/**
 * 转化前的节点
 */
public abstract class TreeSourceNode {

    // 获取节点ID
    public abstract String getId();

    // 获取节点名称
    public abstract String getName();

    // 获取父节点ID
    public abstract String getParentId();

}
import java.util.List;

/**
 * 转化后的树形节点
 */
public class TreeDestNode<T extends TreeSourceNode> {

    // 默认展开的 level
    public static int SPREAD_LEVEL = 2;

    // 唯一标识
    private String id;
    // 名称
    private String name;
    // 级别
    private int level;
    // 是否禁用,默认不禁用
    private boolean disabled = false;
    // 是否默认选中
    private boolean checked = true;
    // 是否默认展开
    private boolean spread = false;
    // 子节点
    private List<TreeDestNode<T>> children;
    // 元数据
    private T meta;

    // ---------------------------------- setter
    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public void setChildren(List<TreeDestNode<T>> children) {
        this.children = children;
    }

    public void setMeta(T meta) {
        this.meta = meta;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public void setSpread(boolean spread) {
        this.spread = spread;
    }

    // ---------------------------------- getter
    public T getMeta() {
        return meta;
    }

    public String getId() {
        return id;
    }

    public boolean isDisabled() {
        return disabled;
    }

    public List<TreeDestNode<T>> getChildren() {
        return children;
    }

    public int getLevel() {
        return level;
    }

    public boolean isChecked() {
        return checked;
    }

    public boolean isSpread() {
        return spread;
    }

    // name -> title
    public String getTitle() {
        return name;
    }

}
import java.util.ArrayList;
import java.util.List;

/**
 * 树形数据工具类
 */
public class TreeHelper {

    private TreeHelper() {

    }

    public static <T extends TreeSourceNode> List<TreeDestNode<T>> convert(List<T> sourceNodeList) {
        List<TreeDestNode<T>> destNodeList = new ArrayList<TreeDestNode<T>>();
        // 第一步,找出第一级的节点
        // 1.1 统计所有节点的id
        List<String> allIds = new ArrayList<String>();
        for (T sourceNode : sourceNodeList) {
            allIds.add(sourceNode.getId());
        }
        // 所有父节点找不到对应的都是一级id
        for (T sourceNode : sourceNodeList) {
            if (!allIds.contains(sourceNode.getParentId())) {
                // 从每个一级节点,递归查找children
                TreeDestNode<T> destNode = new TreeDestNode<T>();
                destNode.setId(sourceNode.getId());
                destNode.setName(sourceNode.getName());
                destNode.setLevel(1);
                destNode.setMeta(sourceNode);
                if (TreeDestNode.SPREAD_LEVEL >= destNode.getLevel()) {
                    destNode.setSpread(true);
                }
                List<TreeDestNode<T>> myChilds = getChilderen(sourceNodeList, destNode);
                destNode.setChildren(myChilds.isEmpty() ? null : myChilds);
                destNodeList.add(destNode);
            }
        }
        return destNodeList;
    }

    // 递归获取子节点
    private static <T extends TreeSourceNode> List<TreeDestNode<T>> getChilderen(List<T> sourceNodeList, TreeDestNode<T> parentNode) {
        List<TreeDestNode<T>> childrenList = new ArrayList<TreeDestNode<T>>();
        for (T sourceNode : sourceNodeList) {
            if (sourceNode.getParentId().equals(parentNode.getId())) {
                TreeDestNode<T> children = new TreeDestNode<T>();
                children.setId(sourceNode.getId());
                children.setName(sourceNode.getName());
                children.setLevel(parentNode.getLevel() + 1);
                children.setMeta(sourceNode);
                if (TreeDestNode.SPREAD_LEVEL >= children.getLevel()) {
                    children.setSpread(true);
                }
                List<TreeDestNode<T>> myChilds = getChilderen(sourceNodeList, children);
                children.setChildren(myChilds.isEmpty() ? null : myChilds);
                childrenList.add(children);
            }
        }
        return childrenList;
    }
}

用法:
1、新建类,extends TreeSourceNode
2、设置 id、name、parentId
3、调用List<TreeDestNode<VO>> destNodeList = TreeHelper.convert(VOs);

Java  后端 
更新时间:2022-07-12 09:47:08

本文由 新逸Cary 创作,如果您觉得本文不错,请随意赞赏
采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
原文链接:https://blog.xinac.cn/archives/java-tree-node.html
最后更新:2022-07-12 09:47:08

评论

Your browser is out of date!

Update your browser to view this website correctly. Update my browser now

×