Java递归示例:实现文档树

本文介绍了一种使用递归方法实现文档树结构的技术方案。通过递归查询数据库并构建前端所需的文档树结构,适用于多种场景。同时提供了具体的代码示例,包括如何将扁平化的数据转换成树形结构。

1、递归代码示例

/**
  * 按条件查询所有的文件
  */
    @Override
    public List<DocinfDocBean> getAllDocAsRoot(long securityLevel) throws Exception {
        String hql = "from DocinfDoc o where 1=1 and o.securityLevel <= " + securityLevel + " and o.docinfDoc.cod is null ";
        List<DocinfDoc> rows=baseDao.query(hql);
        try {
            List<DocinfDocBean> docinfDocBeans = new ArrayList<DocinfDocBean>();
            for (DocinfDoc row : rows) {
                DocinfDocBean doc = covertBean(row);
                docinfDocBeans.add(doc);
            }
            return getDocList(docinfDocBeans, securityLevel);
        } catch (Exception e) {
            logger.error("查询文档异常");
        }
        return null;
    }

    private List<DocinfDocBean> getDocList(List<DocinfDocBean> list,long securityLevel){
        for(DocinfDocBean docinfo : list){
            String hql = "from DocinfDoc o where 1=1 and o.securityLevel <= " + securityLevel + " and o.docinfDoc.cod = '"+docinfo.getId()+"'";
            List<DocinfDoc> rows=baseDao.query(hql);
            List<DocinfDocBean> children = new ArrayList<DocinfDocBean>();
            for (DocinfDoc row : rows) {
                DocinfDocBean doc = covertBean(row);
                children.add(doc);
            }
            docinfo.setChildren(children);
            getDocList(children,securityLevel);
        }
        return list;
    }

2、该示例用于实现文档树。前端easyui treegrid
3、说明:文档树的实现方式很多,递归只是其一,具体用哪种方式,要根据项目的具体情况。
4、用递归实现将将List转换为List的工具类
说明:List中的Object对象中不包含children信息。

public class TreeUtil {

private static TreeUtil treeUtil;

private TreeUtil(){

}

public static TreeUtil getInstance(){
    if (treeUtil == null) {
        treeUtil = new TreeUtil();
    }
    return treeUtil;
}

public List<Tree> Convert(Map<String, Tree> nodes) {
    List<Tree> trees = new ArrayList<Tree>();
    Map<String, List<Tree>> ChildrensMap = new HashMap<String, List<Tree>>();

    Iterator<Entry<String, Tree>> iterator = nodes.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry entry = (Map.Entry) iterator.next();
        Tree node = (Tree) entry.getValue();

        if (node.getPid() == null) {
            trees.add(node);
        }else {
            List<Tree> chilrens = ChildrensMap.get(node.getPid());
            if (chilrens == null) {
                chilrens = new ArrayList<Tree>();
                chilrens.add(node);
            }else {
                int index = 0;
                if (node.getSeq() != null) {
                    while (index < chilrens.size()) {
                        if (chilrens.get(index).getSeq() > node.getSeq()) {
                            break;
                        }
                        index++;
                    }
                }

                chilrens.add(index, node);
            }
            ChildrensMap.put(node.getPid(), chilrens);
        }
    }

    for (Tree root : trees) {
        recursiveTree(root.getId(), ChildrensMap, nodes);
    }
    Collections.sort(trees);

    return trees;
}

private Tree recursiveTree(String id, Map<String, List<Tree>> Childrens,Map<String, Tree> nodes) {
    //根据id获取节点对象
    Tree parent = nodes.get(id);
    //查询id下的所有子节点
    List<Tree> Children = Childrens.get(id); 

    //遍历子节点
    if (Children != null) {
        for(Tree child : Children){
            Tree n = recursiveTree(child.getId(), Childrens, nodes); //递归
            parent.setChildren(Children);
        }
    }

    return parent;
}
}

5、TreeUtil使用示例代码

@Override
public List<Tree> getMenu() throws Exception {
    StringBuffer hql = new StringBuffer(" from Resource o where 1 = 1 ");
    StringBuffer paramter = new StringBuffer();
    paramter.append(" and o.level < 4 and o.cls = 'Menu' and o.status > 0 ");
    hql.append(paramter);
    List<Resource> rows = query(hql.toString());
    Map<String, Tree> nodes = new HashMap<String, Tree>();
    if(rows != null && rows.size() > 0){
           for(Resource res : rows){
               nodes.put(res.getId().toString(), ConvertResourceBean(res));
           }
       }
       List<Tree> treeList = TreeUtil.getInstance().Convert(nodes);
    return treeList
}

另外一编递归文章 Java递归示例2:实现区域树

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值