构建树形结构

构建树形结构

1.平铺数据

private static List<Map> menuList(){
    List<Map> menus = new ArrayList<>();
    Map menu1 = new HashMap();
    menu1.put("parentId",0);
    menu1.put("name","系统设置");
    menu1.put("id",1);
    menu1.put("childList",new ArrayList<>());
    Map menu2 = new HashMap();
    menu2.put("parentId",0);
    menu2.put("name","系统监控");
    menu2.put("id",2);
    menu2.put("childList",new ArrayList<>());
    Map menu3 = new HashMap();
    menu3.put("parentId",1);
    menu3.put("name","用户管理");
    menu3.put("id",3);
    menu3.put("childList",new ArrayList<>());
    Map menu4 = new HashMap();
    menu4.put("parentId",3);
    menu4.put("name","用户列表");
    menu4.put("id",4);
    menu4.put("childList",new ArrayList<>());
    menus.add(menu1);
    menus.add(menu2);
    menus.add(menu3);
    menus.add(menu4);
    return menus;
}
/**[{"name":"系统设置","childList":[],"id":1,"parentId":0},{"name":"系统监控","childList":[],"id":2,"parentId":0},{"name":"用户管理","childList":[],"id":3,"parentId":1},{"name":"用户列表","childList":[],"id":4,"parentId":3}]*/

2.得到子节点列表

/**
* list 数据
* t 当前节点(父节点)
*/
private static List<Map> getChildMapList(List<Map> list, Map t)
{
    List<Map> tlist = new ArrayList<Map>();
    Iterator<Map> it = list.iterator();
    while (it.hasNext())
    {
        Map n = (Map) it.next();
        if (Integer.parseInt(n.get("parentId")+"") == Integer.parseInt(t.get("id")+""))
        {
            tlist.add(n);
        }
    }
    return tlist;
}

3.递归列表

private static void recursionFnMap(List<Map> list, Map t)
{
    // 得到子节点列表
    List<Map> childList = getChildMapList(list, t);
    t.put("childList",childList);
    for (Map tChild : childList)
    {
        if (getChildMapList(list, t).size() > 0)
        {
            recursionFnMap(list, tChild);
        }
    }
}

4.构建树结构

 /**
   * @param menus 菜单列表
   * @return 树结构列表
   */
public static List<Map> buildMapTree(List<Map> menus)
{
    List<Map> returnList = new ArrayList<Map>();
    List<Integer> tempList = new ArrayList<Integer>();
    for (Map dept : menus)
    {
        tempList.add(Integer.parseInt(dept.get("id")+""));
    }
    for (Iterator<Map> iterator = menus.iterator(); iterator.hasNext();)
    {
        Map menu = (Map) iterator.next();
        // 如果是顶级节点, 遍历该父节点的所有子节点
        if (!tempList.contains(Integer.parseInt(menu.get("parentId")+"")))
        {
            recursionFnMap(menus, menu);
            returnList.add(menu);
        }
    }
    if (returnList.isEmpty())
    {
        returnList = menus;
    }
    return returnList;
}
5.运行
public static void main(String[] args) {
        List<Map> list = menuList();
        System.out.println("-------平铺展示-------\n"+JSONUtil.toJsonStr(list));
        List<Map> mapList = buildMapTree(list);
        System.out.println("\n-------树形展示-------\n"+JSONUtil.toJsonStr(mapList));

}
-- run result
-------平铺展示-------
[{"name":"系统设置","childList":[],"id":1,"parentId":0},{"name":"系统监控","childList":[],"id":2,"parentId":0},{"name":"用户管理","childList":[],"id":3,"parentId":1},{"name":"用户列表","childList":[],"id":4,"parentId":3}]

-------树形展示-------
[{"name":"系统设置","childList":[{"name":"用户管理","childList":[{"name":"用户列表","childList":[],"id":4,"parentId":3}],"id":3,"parentId":1}],"id":1,"parentId":0},{"name":"系统监控","childList":[],"id":2,"parentId":0}]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值