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;
        }
    
  • 相关阅读:
    [刘阳Java]_eayui-pagination分页组件_第5讲
    [刘阳Java]_easyui-draggable拖动组件_第4讲
    [刘阳Java]_easyui-panel组件入门级_第3讲
    [刘阳Java]_TortoiseSVN基础应用_第1讲
    [刘阳Java]_SpringMVC文件上传第2季_第11讲
    [刘阳Java]_Spring中IntrospectorCleanupListener的用途【补充】_第16讲
    使用fetch代替ajax请求 post传递方式
    react购物车demo
    react-redux异步数据操作
    redux模块化demo
  • 原文地址:https://www.cnblogs.com/wl1202/p/15410147.html
Copyright © 2011-2022 走看看