https://blog.csdn.net/cenguocheng/article/details/81194244
理解:按照树状图的方式
0级,可以当成树干。
1级,从树干上分出来的树枝,假如说没有“childMenus"就是这个树枝是光秃秃的; 如果有“childMenus"就是这个树枝上又分出来了小树枝,此时可以理解为2级。
然后一直这样的反复反复。
当然需要一个判断条件去检索当前的这个树枝还有没有树枝,没有树枝了就返回 。
小小小小树枝,把结果返回到小小小树枝,然后返回到小小树枝,然后返回到小树枝,然后返回到树枝。最后手动加上一个根“menu”。
这样得到的结果就是一颗树。
{
"menu": [{
"childMenus": [{
"childMenus": [{
"id": "4",
"name": "4",
"order": 0,
"parentId": "2"
}, {
"childMenus": [{
"childMenus": [{
"id": "9",
"name": "9",
"order": 0,
"parentId": "10"
}],
"id": "10",
"name": "10",
"order": 0,
"parentId": "6"
}],
"id": "6",
"name": "6",
"order": 0,
"parentId": "2"
}, {
"childMenus": [{
"id": "11",
"name": "11",
"order": 0,
"parentId": "7"
}],
"id": "7",
"name": "7",
"order": 0,
"parentId": "2"
}],
"id": "2",
"name": "2",
"order": 0,
"parentId": "1"
}, {
"childMenus": [{
"id": "8",
"name": "8",
"order": 0,
"parentId": "3"
}],
"id": "3",
"name": "3",
"order": 0,
"parentId": "1"
}],
"id": "1",
"name": "1",
"order": 0,
"parentId": "0"
}, {
"id": "5",
"name": "5",
"order": 0,
"parentId": "0"
}]
}
package com.xz.TreeSimple.TreeTest;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Classname TestTree
* @Description
* @Date 2019/7/3 10:20
* @Created by admin
*/
public class TestTree {
/**
* 初始化数据
* @return
*/
public static List<Menu> initData(){
List<Menu> treeData = new ArrayList<Menu>();
treeData.add(new Menu("1","1","0"));
treeData.add(new Menu("2","2","1"));
treeData.add(new Menu("3","3","1"));
treeData.add(new Menu("4","4","2"));
treeData.add(new Menu("5","5","0"));
treeData.add(new Menu("6","6","2"));
treeData.add(new Menu("7","7","2"));
treeData.add(new Menu("8","8","3"));
treeData.add(new Menu("9","9","10"));
treeData.add(new Menu("10","10","6"));
treeData.add(new Menu("11","11","7"));
return treeData;
}
/**
* 子菜单递归
* @param id
* @param rootMenu
* @return
*/
private static List<Menu> getChild(String id, List<Menu> rootMenu) {
// 子菜单
List<Menu> childList = new ArrayList<>();
for (Menu menu : rootMenu) {
// 遍历所有节点,将父菜单id与传过来的id比较
if (menu.getParentId().equals(id)) {
childList.add(menu);
}
}
// 把子菜单的子菜单再循环一遍,内部也是用的childelist存储。
for (Menu menu : childList) {
menu.setChildMenus(getChild(menu.getId(), rootMenu));// 递归
}
// 判断递归结束
//更细致的说应该是在二级的子菜单判断完毕后,就校验下二级里面有没有数据存在,如果二级里面没有那么就返回一个null。
//不能放在上面的两个代码中中间,是因为如果第4、5级有空值了,就直接返回了,之前的判断就莫名其妙的给扔了。
if (childList.size() == 0) {
return null;
}
return childList;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Menu> treeData = initData(); // 原始数据
// 查看结果
for (Menu menu : treeData) {
System.out.println(menu.toString());
}
List<Menu> menuList = new ArrayList<Menu>(); // 树递归
// 先找到所有的一级菜单
for (int i = 0; i < treeData.size(); i++) {
// 一级菜单父ID为0
if (treeData.get(i).getParentId().equals("0")) {
menuList.add(treeData.get(i));
}
}
// 为一级菜单设置子菜单,getChild是递归调用的
for (Menu menu : menuList) {
List<Menu> listR= getChild(menu.getId(), treeData);
menu.setChildMenus(listR);
}
Map<String,Object> jsonMap = new HashMap<String,Object>();
jsonMap.put("menu", menuList);
System.out.println(JSON.toJSONString(jsonMap));
/* 这个是生成菜单按钮
buildMenu(menuList);
System.out.println(str);*/
}
/**
* 遍历菜单
* @param arr
* @return
*/
/* public static String str = "";
public static String buildMenu(List<Menu> arr){
for(int i=0;i<arr.size();i++){
str += "<li>";
str += "<a href='javascript:;'>" +
"<cite>"+arr.get(i).getName()+"</cite>" +
"<i class='iconfont nav_right'>"+arr.get(i).getIcon()+"</i>" +
"</a>";
//存在子菜单 递归
if(arr.get(i).getChildMenus() != null && arr.get(i).getChildMenus().size() > 0){
str += "<ul>";
buildMenu(arr.get(i).getChildMenus()); // 递归
str += "</ul>";
}
str += "</li>";
}
return str;
}*/
}
package com.xz.TreeSimple.TreeTest;
import java.util.List;
/**
* @Classname Menu
* @Description TODO
* @Date 2019/7/3 10:18
* @Created by admin
*/
public class Menu {
// 菜单id
private String id;
// 菜单名称
private String name;
// 父菜单id
private String parentId;
// 菜单url
private String url;
// 菜单图标
private String icon;
// 菜单顺序
private int order;
// 子菜单
private List<Menu> childMenus;
public Menu(String id, String name, String parentId) {
super();
this.id = id;
this.name = name;
this.parentId = parentId;
}
@Override
public String toString() {
return "Menu [id=" + id + ", name=" + name + ", parentId=" + parentId
+ ", url=" + url + ", icon=" + icon + ", order=" + order
+ ", childMenus=" + childMenus + "]";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public List<Menu> getChildMenus() {
return childMenus;
}
public void setChildMenus(List<Menu> childMenus) {
this.childMenus = childMenus;
}
}