Java 加载、处理树状对象
因查询业务需要,记录一个加载树状对象的实现思路
1,新增一个树状对象
public class orgTreeModel implements Serializable { private static final long serialVersionUID = 1L; /** 对应id字段,前端数据树中的key*/ private String key; /** 对应id字段,前端数据树中的value*/ private String value; /** 对应name字段,前端数据树中的title*/ private String title; private boolean isLeaf; private String pid; List<orgTreeModel> children = new ArrayList<>(); /** * 将第三方模型的部分数据放在该对象当中 * @param treeModel * @return */ public orgTreeModel convert(TbCorporateBaseinfo treeModel) { this.key = treeModel.getId(); this.value = treeModel.getId(); this.pid =treeModel.getPid(); this.title = treeModel.getOrganization(); return this; } public orgTreeModel convert2(orgTreeModel treeModel) { this.key = treeModel.getKey(); this.value = treeModel.getValue(); this.pid =treeModel.getPid(); this.title = treeModel.getTitle(); return this; } }
2,实现:
/** * 方法 ====1===== * 该方法是将自定义类型的list集合转换成orgTreeModel类型的集合
* recordList是未经过处理的数据集,对象中有个字段pid是父子级联关系 */ public static List<orgTreeModel> wrapTreeDataToTreeList(List<TbCorporateBaseinfo> recordList) { List<orgTreeModel> records = new ArrayList<>(); for (int i = 0; i < recordList.size(); i++) { TbCorporateBaseinfo tempM = recordList.get(i); records.add(new orgTreeModel().convert(tempM)); } List<orgTreeModel> tree = findChildren(records); setEmptyChildrenAsNull(tree); return tree; } /** * queryTreeList的子方法 ====2===== * 该方法是找到并封装顶级父类的节点到TreeList集合 */ private static List<orgTreeModel> findChildren(List<orgTreeModel> recordList) { List<orgTreeModel> departIdList = new ArrayList<>(); List<orgTreeModel> treeList = new ArrayList<>(); for (int i = 0; i < recordList.size(); i++) { orgTreeModel branch = recordList.get(i); if (oConvertUtils.isEmpty(branch.getPid()) || "0".equals(branch.getPid())) { treeList.add(branch); orgTreeModel departIdModel = new orgTreeModel().convert2(branch); departIdList.add(departIdModel); } } getGrandChildren(treeList,recordList,departIdList); //idList = departIdList; return treeList; } /** * queryTreeList的子方法====3==== *该方法是找到顶级父类下的所有子节点集合并封装到TreeList集合 */ private static void getGrandChildren(List<orgTreeModel> treeList,List<orgTreeModel> allList,List<orgTreeModel> idList) { for (int i = 0; i < treeList.size(); i++) { orgTreeModel model = treeList.get(i); orgTreeModel idModel = idList.get(i); for (int i1 = 0; i1 < allList.size(); i1++) { orgTreeModel m = allList.get(i1); if (m.getPid()!=null && m.getPid().equals(model.getKey())) { model.getChildren().add(m); orgTreeModel dim = new orgTreeModel().convert2(m); idModel.getChildren().add(dim); } } getGrandChildren(treeList.get(i).getChildren(), allList, idList.get(i).getChildren()); } } /** * queryTreeList的子方法 ====4==== * 该方法是将子节点为空的List集合设置为Null值 */ private static void setEmptyChildrenAsNull(List<orgTreeModel> treeList) { for (int i = 0; i < treeList.size(); i++) { orgTreeModel model = treeList.get(i); if (model.getChildren().size() == 0) { model.setChildren(null); model.setIsLeaf(true); }else{ setEmptyChildrenAsNull(model.getChildren()); model.setIsLeaf(false); } } // sysDepartTreeList = treeList; }
ex:统计最末节点的数量
public static int doCount(orgTreeModel root){ int count = 0; List<orgTreeModel> list = root.getChildren(); if(list==null ||list.size()==0){ return count; } for (orgTreeModel child : list) { //统计当前元素的子节点个数(最末节点判断) if(child.getIsLeaf()){ count++; } //统计子节点的孩子总数 int cur_cnt = 0 ; if(!child.getIsLeaf()){ cur_cnt = doCount(child); } count += cur_cnt; } //返回前记录当前节点的统计个数 root.setTitle(String.format(root.getTitle()+"(%d)",count)); return count; }