java递归工具类

/**
 * author :sq
 */
public class TreeDakou {

    private List data = new ArrayList();
    private List nodes;

    public TreeDakou(List menus){
        nodes = new ArrayList();
        for (SysMenu menu : menus) {
            Node node = new Node();
            node.setId(menu.getMenuId());
            node.setParentId(menu.getParentId());
            node.setText(menu.getMenuName());
            node.setHref(menu.getMenuUrl());
            node.setIcon(menu.getMenuIcon());
            NodeState state = new NodeState();
            if(menu.getMuflag()!=null && menu.getMuflag()>0){
                state.setChecked(true);
            }
            node.setState(state);
            nodes.add(node);
        }
        this.nodes = nodes;
    }


    public List buildTree(){
        for (Node node : nodes) {
            if (node.getParentId() == null) {
                build(node);
                data.add(node);
            }
        }
        return data;
     }

    private void build(Node node){
        List children = getChildren(node);
        if (!children.isEmpty()) {
            node.setNodes(children);
            for (Node child : children) {
                build(child);
            }

        }
    }


    private List getChildren(Node node){
        List children = new ArrayList();
        for (Node child : nodes) {
            Integer id = node.getId();
            if (id.equals(child.getParentId())) {
                children.add(child);
             }
        }
        return children;
    }
}

你可能感兴趣的:(java)