Java List<Map<String, Object>>生成树

/**
 *
 * @param list 所有元素的平级集合,map包含id和pid
 * @param pid 顶级节点的pid,可以为null
 * @param idName id位的名称,一般为id或者code
 * @return 树
 */
public static List<Map<String, Object>> getTree(List<Map<String, Object>> list, String pid, String idName) {
    List<Map<String, Object>> res = new ArrayList<Map<String,Object>>();
    if (CollectionUtils.isNotEmpty(list))
        for (Map<String, Object> map : list) {
            if(pid == null && map.get("p"+idName) == null || map.get("p"+idName) != null && map.get("p"+idName).equals(pid)){
                String id = (String) map.get(idName);
                map.put("children", getTree(list, id, idName));
                res.add(map);
            }
        }
    return res;
}


测试代码:

List<String> pcodes = new ArrayList<>();
pcodes.add(null);
List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0, len = 10; i < len; i++){
    Map<String, Object> map = new HashedMap();
    map.put("code", "code" + i);
    map.put("name", "name" + i);
    map.put("pcode", pcodes.get(0));
    pcodes.add("code" + i);
    Collections.shuffle(pcodes);
    list.add(map);
}

System.out.println(CommonUtils.getTree(list, null, "code"));


输出结果:

[
    {
        pcode=null,
        code=code0,
        name=name0,
        children=[
            {
                pcode=code0,
                code=code1,
                name=name1,
                children=[
                    {
                        pcode=code1,
                        code=code4,
                        name=name4,
                        children=[
                            
                        ]
                    },
                    {
                        pcode=code1,
                        code=code5,
                        name=name5,
                        children=[
                            
                        ]
                    }
                ]
            }
        ]
    },
    {
        pcode=null,
        code=code2,
        name=name2,
        children=[
            {
                pcode=code2,
                code=code6,
                name=name6,
                children=[
                    {
                        pcode=code6,
                        code=code7,
                        name=name7,
                        children=[
                            
                        ]
                    },
                    {
                        pcode=code6,
                        code=code8,
                        name=name8,
                        children=[
                            {
                                pcode=code8,
                                code=code9,
                                name=name9,
                                children=[
                                    
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        pcode=null,
        code=code3,
        name=name3,
        children=[
            
        ]
    }
]



  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值