Java代码实现封装多级树结构对象

前言:

在开发中,我们经常见到,前端展示树状结构的,这时候就需要后端去封装一个多级树结构对象,前端根据这样结构的数据去渲染数据,这篇文章讲的是如何封装成多级树结构对象。

正文:

1.先封装个树结构的对象


 
 
  1. @Data
  2. public class TreeDto {
  3. private String id;
  4. private String name;
  5. private String pid;
  6. private String isParent;
  7. private List <TreeDto> childTreeDto;
  8. }

2.然后我把工具类代码粘贴下


 
 
  1. public class TreeToolUtils {
  2. private List <TreeDto> rootList; //根节点对象存放到这里
  3. private List <TreeDto> bodyList; //其他节点存放到这里,可以包含根节点
  4. public TreeToolUtils(List <TreeDto> rootList, List <TreeDto> bodyList) {
  5. this.rootList = rootList;
  6. this.bodyList = bodyList;
  7. }
  8. public List <TreeDto> getTree(){ //调用的方法入口
  9. if(bodyList != null && !bodyList.isEmpty()){
  10. //声明一个map,用来过滤已操作过的数据
  11. Map <String,String> map = Maps.newHashMapWithExpectedSize(bodyList.size());
  12. rootList.forEach(beanTree -> getChild(beanTree,map));
  13. return rootList;
  14. }
  15. return null;
  16. }
  17. public void getChild(TreeDto treeDto,Map <String,String> map){
  18. List <TreeDto> childList = Lists.newArrayList();
  19. bodyList.stream()
  20. .filter(c -> !map.containsKey(c.getId()))
  21. .filter(c ->c.getPid().equals(treeDto.getId()))
  22. .forEach(c ->{
  23. map.put(c.getId(),c.getPid());
  24. getChild(c,map);
  25. childList.add(c);
  26. });
  27. treeDto.setChildTreeDto(childList);
  28. }
  29. }

3.然后写个main方法来测试下


 
 
  1. TreeDto treeDto = new TreeDto("1", "总店", "null", "true",null);
  2. TreeDto treeDto1 = new TreeDto("2", "市分店", "1", "true",null);
  3. TreeDto treeDto2 = new TreeDto("3", "县分店", "2", "true",null);
  4. TreeDto treeDto3 = new TreeDto("710", "店长", "3", "true",null);
  5. TreeDto treeDto4= new TreeDto("713", "财务部", "3", "true",null);
  6. TreeDto treeDto5 = new TreeDto("20032", "后勤部", "3", "true",null);
  7. TreeDto treeDto6 = new TreeDto("1909", "小王", "710", "false",null);
  8. TreeDto treeDto7= new TreeDto("1974", "小张", "713", "false",null);
  9. TreeDto treeDto8 = new TreeDto("388187", "佳佳", "20032", "false",null);
  10. TreeDto treeDto9 = new TreeDto("1949", "阿达", "20032", "false",null);
  11. ArrayList <TreeDto> rootList = new ArrayList <>();//根节点
  12. ArrayList <TreeDto> bodyList = new ArrayList <>();//子节点
  13. rootList.add(treeDto);
  14. bodyList.add(treeDto1);
  15. bodyList.add(treeDto2);
  16. bodyList.add(treeDto3);
  17. bodyList.add(treeDto4);
  18. bodyList.add(treeDto5);
  19. bodyList.add(treeDto6);
  20. bodyList.add(treeDto7);
  21. bodyList.add(treeDto8);
  22. bodyList.add(treeDto9);
  23. TreeToolUtils utils = new TreeToolUtils(rootList,bodyList);
  24. List <TreeDto> result = utils.getTree();
  25. String jsonString = JSONObject.toJSONString(result.get(0));
  26. System.out.println(jsonString);
  27. }

4.最后控制台打印出的结果格式化后,就是这样的数据啦,前端根据层级去渲染数据就行啦

{
    "childTreeDto": [{
        "childTreeDto": [{
            "childTreeDto": [{
                "childTreeDto": [{
                    "childTreeDto": [],
                    "id": "1909",
                    "isParent": "false",
                    "name": "小王",
                    "pid": "710"
                }],
                "id": "710",
                "isParent": "true",
                "name": "店长",
                "pid": "3"
            }, {
                "childTreeDto": [{
                    "childTreeDto": [],
                    "id": "1974",
                    "isParent": "false",
                    "name": "小张",
                    "pid": "713"
                }],
                "id": "713",
                "isParent": "true",
                "name": "财务部",
                "pid": "3"
            }, {
                "childTreeDto": [{
                    "childTreeDto": [],
                    "id": "388187",
                    "isParent": "false",
                    "name": "佳佳",
                    "pid": "20032"
                }, {
                    "childTreeDto": [],
                    "id": "1949",
                    "isParent": "false",
                    "name": "阿达",
                    "pid": "20032"
                }],
                "id": "20032",
                "isParent": "true",
                "name": "后勤部",
                "pid": "3"
            }],
            "id": "3",
            "isParent": "true",
            "name": "县分店",
            "pid": "2"
        }],
        "id": "2",
        "isParent": "true",
        "name": "市分店",
        "pid": "1"
    }],
    "id": "1",
    "isParent": "true",
    "name": "总店",
    "pid": "null"
}

总结:

我是阿达,一名喜欢分享知识的程序员,时不时的也会荒腔走板的聊一聊电影、电视剧、音乐、漫画,这里已经有十位小伙伴在等你们啦,感兴趣的就赶紧来点击关注我把,哪里不明白或有不同观点的地方欢迎留言。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
$(function(){ $.fn.extend({ SimpleTree:function(options){ //初始化参数 var option = $.extend({ click:function(a){ } },options); option.tree=this; /* 在参数对象中添加对当前菜单树的引用,以便在对象中使用该菜单树 */ option._init=function(){ /* * 初始化菜单展开状态,以及分叉节点的样式 */ this.tree.find("ul ul").hide(); /* 隐藏所有子级菜单 */ this.tree.find("ul ul").prev("li").removeClass("open"); /* 移除所有子级菜单父节点的 open 样式 */ this.tree.find("ul ul[show='true']").show(); /* 显示 show 属性为 true 的子级菜单 */ this.tree.find("ul ul[show='true']").prev("li").addClass("open"); /* 添加 show 属性为 true 的子级菜单父节点的 open 样式 */ }/* option._init() End */ /* 设置所有超链接不响应单击事件 */ this.find("a").click(function(){ $(this).parent("li").click(); return false; }); /* 菜单项 接受单击 */ this.find("li").click(function(){ /* * 当单击菜单项 * 1.触发用户自定义的单击事件,将该 标签中的第一个超链接做为参数传递过去 * 2.修改当前菜单项所属的子菜单的显示状态(如果等于 true 将其设置为 false,否则将其设置为 true) * 3.重新初始化菜单 */ option.click($(this).find("a")[0]); /* 触发单击 */ /* * 如果当前节点下面包含子菜单,并且其 show 属性的值为 true,则修改其 show 属性为 false * 否则修改其 show 属性为 true */ /* if($(this).next("ul").attr("show")=="true"){ $(this).next("ul").attr("show","false"); }else{ $(this).next("ul").attr("show","true"); }*/ /* 初始化菜单 */ option._init(); }); /* 设置所有父节点样式 */ this.find("ul").prev("li").addClass("folder"); /* 设置节点“是否包含子节点”属性 */ this.find("li").find("a").attr("hasChild",false); this.find("ul").prev("li").find("a").attr("hasChild",true); /* 初始化菜单 */ option._init(); }/* SimpleTree Function End */ }); });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值