Bootstrap实践之Java递归转换treeview结构数据

Bootstraptree菜单
placeholder image
admin 发布于:2020-04-19 02:36:27
阅读:loading

前文中我表示treeview使用的数据格式,需要多层级节点嵌套的结构,而非所有菜单均为一级的数据格式,在使用的时候,我们不得不需要将一个一层结构的数据再转换成多层级的实现,参考如下数据所示:

初始数据结构

[
  { 
"id"1"name""北京市""parentId"-1 },
  { 
"id"2"name""海淀区""parentId"},
  { 
"id"3"name""朝阳区""parentId"},
  { 
"id"4"name""湖北省""parentId"-1 },
  { 
"id"5"name""武汉市""parentId"},
  { 
"id"6"name""襄阳市""parentId"},
  { 
"id"7"name""襄城区""parentId"},
  { 
"id"8"name""襄州区""parentId"},
  { 
"id"9"name""樊城区""parentId"},
  { 
"id"10"name""老河口市""parentId"},
  { 
"id"11"name""谷城县""parentId"},
  { 
"id"12"name""南漳县""parentId"},
  { 
"id"13"name""枣阳市""parentId"},
  { 
"id"14"name""保康县""parentId"},
  { 
"id"15"name""宜城市""parentId"}
]

 

treeview需要的数据结构

[
  {
   
"nodeId": 1,
   
"nodes": [
      {
       
"nodeId": 2,
       
"text": "海淀区"
     
},
      {
        
"nodeId": 3,
       
"text": "朝阳区"
     
}
    ],
   
"text": "北京市"
 
},
  {
   
"nodeId": 4,
   
"nodes": [
      {
       
"nodeId": 5,
       
"text": "武汉市"
     
},
      {
       
"nodeId": 6,
       
"nodes": [
          {
           
"nodeId": 7,
           
"text": "襄城区"
         
},
          {
           
"nodeId": 8,
           
"text": "襄州区"
         
},
          {
           
"nodeId": 9,
           
"text": "樊城区"
         
},
          {
           
"nodeId": 10,
           
"text": "老河口市"
         
},
          {
           
"nodeId": 11,
           
"text": "谷城县"
         
},
          {
           
"nodeId": 12,
           
"text": "南漳县"
         
},
          {
           
"nodeId": 13,
           
"text": "枣阳市"
         
},
          {
           
"nodeId": 14,
           
"text": "保康县"
         
},
          {
           
"nodeId": 15,
           
"text": "宜城市"
         
}
        ],
       
"text": "襄阳市"
     
}
    ],
   
"text": "湖北省"
 
}
]

参考代码

package cn.chendd.tips.examples.treeview;

import ...;

/**
 *
数据转换测试
 
*
 * @author chendd
 * @date 2020/x/xx 22:34
 */
public class DataConvertTest {

   
private Integer ROOT_ID = -1;//约定根节点为-1
   
private List<City> cityList;

   
@Test
   
public void test(){
        System.
out.println("======原始数据======");
        System.
out.println(JSONObject.toJSONString(cityList , true));
        Stream<City> rootCity =
cityList.stream().filter(city -> ROOT_ID.equals(city.getParentId()));
        List<Treeview> treeviewList =
new ArrayList<>();
       
this.convertTreeview(rootCity, treeviewList);
       
for (Treeview treeview : treeviewList) {
           
this.collectList(treeview , this.cityList);
        }
        System.
out.println("======转换后的数据======");
        System.
out.println(JSON.toJSONString(treeviewList , true));
    }

   
/**
     *
获取原始数据中的一级菜单
    
*/
   
private void convertTreeview(Stream<City> cityStream, List<Treeview> treeviewList) {
        cityStream.forEach(city -> {
            Treeview treeview =
new Treeview();
            treeview.setNodeId(city.getId());
            treeview.setText(city.getName());
            treeview.setState(
null);
            treeview.setNodes(
null);
           
treeviewList.add(treeview);
        });
    }

   
/**
     *
遍历一级菜单,并递归调用字级菜单,查找对应的菜单节点
    
*/
   
private void collectList(Treeview treeview, List<City> cityList) {
        Integer nodeId = treeview.getNodeId();
       
for (City city : cityList) {
            Integer parentId = city.getParentId();
           
if(parentId != null && parentId.equals(nodeId)){
               
if(treeview.getNodes() == null){
                    treeview.setNodes(
new ArrayList<>());
                }
                Treeview childMenuTreeview =
new Treeview();
                childMenuTreeview.setNodeId(city.getId());
                childMenuTreeview.setText(city.getName());
                childMenuTreeview.setState(
null);
                treeview.getNodes().add(childMenuTreeview);
               
this.collectList(childMenuTreeview , cityList);
            }
        }
    }

   
@Before
   
public void init(){
       
cityList = new ArrayList<>();
       
cityList.add(new City(1 , "北京市" , ROOT_ID));
       
cityList.add(new City(2 , "海淀区" , 1));
       
cityList.add(new City(3 , "朝阳区" , 1));
       
cityList.add(new City(4 , "湖北省" , ROOT_ID));
       
cityList.add(new City(5 , "武汉市" , 4));
       
cityList.add(new City(6 , "襄阳市" , 4));
       
cityList.add(new City(7 , "襄城区" , 6));
       
cityList.add(new City(8 , "襄州区" , 6));
       
cityList.add(new City(9 , "樊城区" , 6));
       
cityList.add(new City(10 , "老河口市" , 6));
       
cityList.add(new City(11 , "谷城县" , 6));
       
cityList.add(new City(12 , "南漳县" , 6));
       
cityList.add(new City(13 , "枣阳市" , 6));
       
cityList.add(new City(14 , "保康县" , 6));
       
cityList.add(new City(15 , "宜城市" , 6));
    }

}


注意:本程序存在Java后端代码的递归调用,可能会出现由数据的上下级导致的循环调用,即递归溢出,正常来说需要考虑递归的深度问题。当前博客的源代码中存在此类工具类,包含有递归深度的限制,有需要自行查找。


源码下载

https://gitee.com/88911006/chendd-examples/tree/master/tips/test/main/java/cn/chendd/tips/examples/treeview


 点赞


 发表评论

当前回复:作者

 评论列表


留言区