Java树形结构工具类

XINAC 2022-07-1210:17:58来源:https://blog.xinac.cn
评论
44 3120字
摘要

Java树形结构工具类,TreeHelper

/**
 * 转化前的节点
 */
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);

 

weinxin
新逸IT技术
扫一扫关注微信公众号
XINAC
  • 版权声明 本文源自 https://blog.xinac.cn 整理 发表于 2022-07-1210:17:58
  • 转载请注明:https://www.xinac.net/9278.html
Java设计模式十三:模板模式 软件程序

Java设计模式十三:模板模式

模板(Template Method)模式:定义一个操作中的算法骨架,而将算法的一些步骤延迟到子类中,使得子类可以不改变该算法结构的情况下重定义该算法的某些特定步骤。
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定