前言:
在开发中,我们经常见到,前端展示树状结构的,这时候就需要后端去封装一个多级树结构对象,前端根据这样结构的数据去渲染数据,这篇文章讲的是如何封装成多级树结构对象。
正文:
1.先封装个树结构的对象
@Data public class TreeDto { private String id; private String name; private String pid; private String isParent; private List<TreeDto> childTreeDto; }
2.然后我把工具类代码粘贴下
public class TreeToolUtils { private List<TreeDto> rootList; //根节点对象存放到这里 private List<TreeDto> bodyList; //其他节点存放到这里,可以包含根节点 public TreeToolUtils(List<TreeDto> rootList, List<TreeDto> bodyList) { this.rootList = rootList; this.bodyList = bodyList; } public List<TreeDto> getTree(){ //调用的方法入口 if(bodyList != null && !bodyList.isEmpty()){ //声明一个map,用来过滤已操作过的数据 Map<String,String> map = Maps.newHashMapWithExpectedSize(bodyList.size()); rootList.forEach(beanTree -> getChild(beanTree,map)); return rootList; } return null; } public void getChild(TreeDto treeDto,Map<String,String> map){ List<TreeDto> childList = Lists.newArrayList(); bodyList.stream() .filter(c -> !map.containsKey(c.getId())) .filter(c ->c.getPid().equals(treeDto.getId())) .forEach(c ->{ map.put(c.getId(),c.getPid()); getChild(c,map); childList.add(c); }); treeDto.setChildTreeDto(childList); } }
3.然后写个main方法来测试下
TreeDto treeDto = new TreeDto("1", "总店", "null", "true",null); TreeDto treeDto1 = new TreeDto("2", "市分店", "1", "true",null); TreeDto treeDto2 = new TreeDto("3", "县分店", "2", "true",null); TreeDto treeDto3 = new TreeDto("710", "店长", "3", "true",null); TreeDto treeDto4= new TreeDto("713", "财务部", "3", "true",null); TreeDto treeDto5 = new TreeDto("20032", "后勤部", "3", "true",null); TreeDto treeDto6 = new TreeDto("1909", "小王", "710", "false",null); TreeDto treeDto7= new TreeDto("1974", "小张", "713", "false",null); TreeDto treeDto8 = new TreeDto("388187", "佳佳", "20032", "false",null); TreeDto treeDto9 = new TreeDto("1949", "阿达", "20032", "false",null); ArrayList<TreeDto> rootList = new ArrayList<>();//根节点 ArrayList<TreeDto> bodyList = new ArrayList<>();//子节点 rootList.add(treeDto); bodyList.add(treeDto1); bodyList.add(treeDto2); bodyList.add(treeDto3); bodyList.add(treeDto4); bodyList.add(treeDto5); bodyList.add(treeDto6); bodyList.add(treeDto7); bodyList.add(treeDto8); bodyList.add(treeDto9); TreeToolUtils utils = new TreeToolUtils(rootList,bodyList); List<TreeDto> result = utils.getTree(); String jsonString = JSONObject.toJSONString(result.get(0)); System.out.println(jsonString); }
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" }
原文:https://blog.csdn.net/jdk_wangtaida/article/details/87867620