java生成tree结构数据工具类

本文介绍了一个名为`ComboTree`的Java类,用于构建树形结构数据,并提供了一种通过递归方式建立完整树形结构的方法。`ComboTree`类包含了创建树所需的基本属性,如节点ID、文本、状态等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class ComboTree implements Serializable{
    private String id;//:节点ID,对加载远程数据很重要。
    private String text;//:显示节点文本。
    private String parentid;//:节点ID,对加载远程数据很重要。
    private String state;//:节点状态,’open’ 或 ‘closed’,默认:’open’。如果为’closed’的时候,将不自动展开该节点。
    private boolean checked;//:表示该节点是否被选中。
    private String attributes;//: 被添加到节点的自定义属性。
    private List<ComboTree> children;//: 一个节点数组声明了若干节点。

    public String getId() {
        return id;
    }

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

    public String getParentid() {
        return parentid;
    }

    public void setParentid(String parentid) {
        this.parentid = parentid;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public boolean isChecked() {
        return checked;
    }

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

    public String getAttributes() {
        return attributes;
    }

    public void setAttributes(String attributes) {
        this.attributes = attributes;
    }

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

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


}
public class TreeUtils {

    private List<ComboTree> menuList = new ArrayList<ComboTree>();

    public TreeUtils(List<ComboTree> menuList) {
        this.menuList = menuList;
    }

    //建立树形结构
    public List<ComboTree> builTree() {
        List<ComboTree> treeMenus = new ArrayList<ComboTree>();
        for (ComboTree menuNode : getRootNode()) {
            menuNode = buildChilTree(menuNode);
            treeMenus.add(menuNode);
        }
        return treeMenus;
    }

    //递归,建立子树形结构
    private ComboTree buildChilTree(ComboTree pNode) {
        List<ComboTree> chilMenus = new ArrayList<ComboTree>();
        for (ComboTree menuNode : menuList) {
            if (menuNode.getParentid().equals(pNode.getId())) {
                chilMenus.add(buildChilTree(menuNode));
            }
        }
        pNode.setChildren(chilMenus);
        return pNode;
    }

    //获取根节点
    private List<ComboTree> getRootNode() {
        List<ComboTree> rootMenuLists = new ArrayList<ComboTree>();
        for (ComboTree menuNode : menuList) {
        	//800000000000:顶层父节点,可以改成动态传入
            if (menuNode.getParentid().equals("800000000000")) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值