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);
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章