@Test
public void diGuiTest(){
TreeNode treeNode = new TreeNode("1","0","一级菜单");
TreeNode treeNode2 = new TreeNode("2","1","二级菜单");
TreeNode treeNode3 = new TreeNode("3","1","二级菜单");
TreeNode treeNode4 = new TreeNode("4","2","三级菜单");
TreeNode treeNode5 = new TreeNode("5","2","三级菜单");
TreeNode treeNode6 = new TreeNode("6","3","三级菜单");
TreeNode treeNode7 = new TreeNode("7","3","三级菜单");
List<TreeNode> treeNodes = Arrays.asList(treeNode, treeNode2, treeNode3, treeNode4, treeNode5, treeNode6, treeNode7);
List<TreeNode> build = this.build(treeNodes, "0");
log.info("tree node : {}",JSONObject.toJSONString(build));
}
private List<TreeNode> build(List<TreeNode> treeNodes,String pid){
List<TreeNode> result = new ArrayList<>();
for (TreeNode treeNode : treeNodes) {
if (treeNode.getPid().equals(pid)){
result.add(treeNode);
List<TreeNode> build = this.build(treeNodes, treeNode.getId());
treeNode.setTreeNodes(build);
}
}
return result;
}
@Data
public class TreeNode{
private String id;
private String pid;
private String name;
public TreeNode(String id, String pid, String name) {
this.id = id;
this.pid = pid;
this.name = name;
}
private List<TreeNode> treeNodes;
}
组织菜单递归遍历
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...