Java :Tree 和 List 集合的相互转化

判断是否存在子集
//判断是否存在子集
    private static boolean ifChilds(List<?> list) {
        boolean flag = false;
        if (list != null && list.size() != 0) {
            flag = true;
        }

        return flag;
    }
List 转换成 Tree
 /**
     * 使用递归方法建树 -- wangmx
     *
     * @param treeEntity
     * @return
     */
    public List<BibookInvestmentPlanEntity> buildByRecursive(List<BibookInvestmentPlanEntity> treeEntity) {
        List<BibookInvestmentPlanEntity> trees = new ArrayList<BibookInvestmentPlanEntity>();
        for (BibookInvestmentPlanEntity en : treeEntity) {
            if ("0".equals(en.getPid())) {
                trees.add(findChildren(en, treeEntity));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点 -- wangmx
     *
     * @param entity
     * @param treeEntity
     * @return
     */
    public BibookInvestmentPlanEntity findChildren(BibookInvestmentPlanEntity entity, List<BibookInvestmentPlanEntity> treeEntity) {
        for (BibookInvestmentPlanEntity it : treeEntity) {
            if (entity.getInvestmentPlanId().equals(it.getPid())) {
                if (entity.getChildren() == null) {
                    entity.setChildren(new ArrayList<BibookInvestmentPlanEntity>());
                }
                entity.getChildren().add(findChildren(it, treeEntity));
            }
        }
        return entity;
    }
Tree 转换成 List
/**
     * 把树 转成list
     * @param lists
     * @return
     */
    private List<BibookInvestmentPlanEntity> getTreeChangeList(List<BibookInvestmentPlanEntity> lists){

        List<BibookInvestmentPlanEntity> list = new ArrayList<>();

        lists.forEach(o->{
            this.treeToList(o,list);
        });

        return list;
    }


    /**
     * 将tree结构数据转成List结构
     * @param list
     * @return
     */
    public void treeToList(BibookInvestmentPlanEntity node,List<BibookInvestmentPlanEntity> list){
        if(list==null){
            list=new ArrayList<BibookInvestmentPlanEntity>();
        }
        //设置当前节点的必要数据
        BibookInvestmentPlanEntity nodeValue= new BibookInvestmentPlanEntity(
                node.getId(),node.getInvestmentPlanId(),node.getCourseNumber(),node.getCourseLevel(),node.getCourseName(),
                node.getPid(),node.getNormTotal(),node.getNormUnilateral(),node.getAdjustTotal(),node.getAdjustUnilateral(),
                node.getManual(),node.getInvestType(),node.getInvestTypeName(),node.getCreatTime(),node.getSubmitSave(),
                node.getCoding(),node.getDataSource(),node.getVersionId(),node.getColum1(),node.getColum2(),
                new ArrayList<BibookInvestmentPlanEntity>()

        );
        list.add(nodeValue);
        //遍历递归子节点
        if(ifChilds(node.getChildren())){
            for (int i = 0; i < node.getChildren().size(); i++) {
                BibookInvestmentPlanEntity node_= node.getChildren().get(i);
                treeToList(node_,list);
            }
        }
    }
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值