zoukankan      html  css  js  c++  java
  • java的List列表转成Tree(树形)结构列表

    直接看借鉴博客:https://blog.csdn.net/massivestars/article/details/53911620/

    由于我的业务没有父子级id两个字段,只有一个层级id字段来分层,如一级数据:01,02,03...,二级数据:0101,0102,0103,0201,...,三级数据:010101,010102...等等,但思路是一样的,我用的是两个for循环搞定,如下:

    /**
         * @Description:list转tree(树形方法)
         * @Author:
         * @Date:2018/12/16 16:19
         **/
        public static List<WkgGroupVo> listToTree(List<WkgGroupVo> wkgGroupVoList){
            List<WkgGroupVo> wkgGroupVoTree = new ArrayList<WkgGroupVo>();
            for(WkgGroupVo wgVo : wkgGroupVoList){
    
                //层级字段数据为两位数,则是一级数据
                if(wgVo.getGpStructurecode().length() == 2){
                    wkgGroupVoTree.add(wgVo);
                }
    
                //循环出子级数据,添加到一级数据
                for(WkgGroupVo subWgVo : wkgGroupVoList){
                    int len = subWgVo.getGpStructurecode().length();
                    String gpStructurecode = subWgVo.getGpStructurecode().substring(0, len -2);
                    if(gpStructurecode.equals(wgVo.getGpStructurecode())){
                        if(wgVo.getWgList() == null){
                            wgVo.setWgList(new ArrayList<WkgGroupVo>());
                        }
                        wgVo.getWgList().add(subWgVo);
                    }
                }
    
            }
    
            return wkgGroupVoTree;
        }
  • 相关阅读:
    为Delphi配置多套环境
    0425-字符输入流FileReader
    使用 IntraWeb (9)
    使用 IntraWeb (7)
    使用 IntraWeb (6)
    使用 IntraWeb (5)
    使用 IntraWeb (4)
    使用 IntraWeb (3)
    使用 IntraWeb (2)
    使用 IntraWeb (1)
  • 原文地址:https://www.cnblogs.com/spll/p/10126974.html
Copyright © 2011-2022 走看看