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;
        }
    
  • 相关阅读:
    ssh时传递环境变量
    linux删除文件后磁盘空间未释放的问题
    gitlab-ci配置疑难备忘
    javac老提示无效的标记
    unity5.6里Baked Lighting下面几个Lighting Mode的解释
    屌爆的xamarin,一人单挑google/apple/windows
    xamarin.droid自己的示例工程有些都装不上模拟器,是因为它的architectures选项没设对
    使用NFC读卡器ACR122u读取银行卡信息
    【转】Gnirehtet – 为 Android 设备提供反向网络连接[Windows、macOS、Linux]
    【转】1分钟学会U盘启动安装Linux系统
  • 原文地址:https://www.cnblogs.com/wl1202/p/15410147.html
Copyright © 2011-2022 走看看