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
    评论
你可以使用递归来生成树结构,而不使用嵌套的循环。下面是一个示例代码: ```java import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class TreeGenerator { public static List<Map<String, Object>> generateTree(List<Map<String, Object>> flatList, String parentIdKey, String idKey) { List<Map<String, Object>> treeList = new ArrayList<>(); Map<Object, List<Map<String, Object>>> childMap = new HashMap<>(); // 将所有节点按照父节点分组 for (Map<String, Object> node : flatList) { Object parentId = node.get(parentIdKey); childMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(node); } // 递归生成树结构 generateChildren(childMap, treeList, null, idKey); return treeList; } private static void generateChildren(Map<Object, List<Map<String, Object>>> childMap, List<Map<String, Object>> parentList, Object parentId, String idKey) { List<Map<String, Object>> children = childMap.get(parentId); if (children != null) { for (Map<String, Object> child : children) { Object childId = child.get(idKey); List<Map<String, Object>> grandChildren = new ArrayList<>(); child.put("children", grandChildren); generateChildren(childMap, grandChildren, childId, idKey); } parentList.addAll(children); } } public static void main(String[] args) { // 示例用法 List<Map<String, Object>> flatList = new ArrayList<>(); Map<String, Object> node1 = new HashMap<>(); node1.put("id", 1); node1.put("parentId", null); flatList.add(node1); Map<String, Object> node2 = new HashMap<>(); node2.put("id", 2); node2.put("parentId", 1); flatList.add(node2); Map<String, Object> node3 = new HashMap<>(); node3.put("id", 3); node3.put("parentId", 1); flatList.add(node3); Map<String, Object> node4 = new HashMap<>(); node4.put("id", 4); node4.put("parentId", 2); flatList.add(node4); List<Map<String, Object>> treeList = generateTree(flatList, "parentId", "id"); System.out.println(treeList); } } ``` 这个示例代码中,`generateTree` 方法接受一个扁平的节点列表 `flatList`,以及两个键的名称:`parentIdKey` 和 `idKey`。它首先将节点按照父节点进行分组,然后通过递归调用 `generateChildren` 方法来构建结构。最后返回生成的结构列表。 你可以根据自己的数据结构和需求进行相应的修改。希望这能帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值