zoukankan      html  css  js  c++  java
  • JAVA List和Tree互转

    list泛型对象中要有pid和当前对象泛型的list Child

    public class BgNatureGroup {
    
        @ApiModelProperty(value = "唯一id", name = "id")
        private String id;
    
        @ApiModelProperty(value = "父id", name = "sPid")
        private String sPid;
    
        List<BgNatureGroup> children = new ArrayList<>();
    
    }
    

    list转Tree

    List<BgNatureGroup> bgNatureGroups = bgNatureGroupDao.selectNatureGroup(param);
    List<BgNatureGroup> natureGroupTree = buildValueTree(bgNatureGroups);
    
    
    /**
         * 构造项目树形结构
         *
         * @param depts
         * @return
         */
        private List<BgNatureGroup> buildValueTree(List<BgNatureGroup> depts) {
            Map<String, BgNatureGroup> entityMap = new HashMap<>();
            List<BgNatureGroup> resultList = new ArrayList<>();
    
            for (BgNatureGroup entity : depts) {
                entity.setChildren(new ArrayList<>());
                entityMap.put(entity.getId(), entity);
            }
    
            for (BgNatureGroup entity : depts) {
                BgNatureGroup parentEntity = entityMap.get(entity.getsPid());
                if (StringUtils.isNull(parentEntity)) {
                    resultList.add(entity);
                    continue;
                }
                parentEntity.getChildren().add(entity);
            }
            for (BgNatureGroup nv:resultList) {
                if(nv.getChildren().size()>0){
                    handleChildren(nv.getChildren());
                }
                if(nv.getChildren().size()==0){
                    nv.setChildren(null);
                }
            }
            return resultList;
        }
    
    
    
        private void handleChildren(List<BgNatureGroup> d){
            for (BgNatureGroup nv:d) {
                if(nv.getChildren().size()>0){
                    handleChildren(nv.getChildren());
                }
                if(nv.getChildren().size()==0){
                    nv.setChildren(null);
                }
            }
        }
    

    Tree转List

    传参为Tree的集合

    public ResponseBean saveGroupTree(List<BgNatureGroup> list) {
            List<BgNatureGroup> natureGroups = treeToList(list, null);
            return new ResponseBean(true, "操作成功", natureGroups);
    
        }
    
    /**
         * 树形结构转list
         *
         * @param 
         * @return
         */
        private List<BgNatureGroup> treeToList(List<BgNatureGroup> depts, String pid) {
    
            List<BgNatureGroup> resultList = new ArrayList<>();
    
            for (BgNatureGroup ip : depts) {
                ip.setsPid(pid);
                if (ip.getChildren() != null && ip.getChildren().size() > 0) {
                    resultList.addAll(treeToList(ip.getChildren(), ip.getId()));
                    ip.setChildren(null);
                    resultList.add(ip);
                } else {
                    resultList.add(ip);
                }
            }
            return resultList;
        }
    
  • 相关阅读:
    像asp.net Mvc一样开发nodejs+express Mvc站点
    js数组方法大全
    自己的时间规划
    7月暑假生活总结
    01. What Is Discrete Mathematics(中英字幕 by Ocean-藏心)
    找工作专题---二分查找
    angular.js 入门基础
    WCF实例管理
    是技术牛人,如何拿到国内IT巨头的Offer
    python
  • 原文地址:https://www.cnblogs.com/wl1202/p/15410147.html
Copyright © 2011-2022 走看看