您的位置 首页 java

java 树结构3 树转化为集合-tree to list

某些业务场景下,需要将树形结构的数据打平,转化为平行的结集合,下面写一种实现方式,当然也是递归实现的。

生成树的方法可以参考文章1:

代码如下:

 private List<SysAreaVo> treeToList(List<SysAreaVo> areatree) {
        List<SysAreaVo> result = new ArrayList<>();
        for (SysAreaVo entity : areatree) {
            result.add(entity);
            List<SysAreaVo> children = entity.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                List<SysAreaVo> entityList = treeToList(children);
                result.addAll(entityList);
            }
        }
        if (!CollectionUtils.isEmpty(result)) {
            result.forEach(r -> r.setChildren(null));
        }
        return result;
    }  
 org.springframework.util.CollectionUtils
spring自带的工具类,判断集合是否为null 或者是否size 为0  

文章来源:智云一二三科技

文章标题:java 树结构3 树转化为集合-tree to list

文章地址:https://www.zhihuclub.com/179257.shtml

关于作者: 智云科技

热门文章

网站地图