找出父节点名称和子节点名称拼接
树形结构
定义一个递归方法来遍历树形结构,每遍历到一个节点,就将其名称与父节点名称进行拼接。
public void getNames(DevDict node, String parentName) {String currentName = parentName.isEmpty() ? node.getDictLabel() : parentName + "-" + node.getDictLabel();for (DevDict child : node.getChildList()) {getAllNames(child, currentName);}// 设置每一个对应的名称node.setAllNames(currentName);System.out.println("currentName = " + currentName);}
测试模拟
// node全部数据
// parentName为空
this.getNames(node, "");
测试结果