You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于将TreeMap按指定ObjectA层级结构转换为List的可行性咨询

解决方案:TreeMap层级结构转嵌套ObjectA列表

当然可以搞定!这种树形结构的转换在日常开发里太常见了,我给你捋清楚思路再上具体代码,保证你能直接用。

先明确ObjectA的结构

首先得把你的ObjectA类定义清楚,要包含存储节点标识的属性和存子节点的subList:

class ObjectA {
    private String code; // 对应01、01-01这类层级编码
    private List<ObjectA> subList = new ArrayList<>();

    // 构造器、getter/setter
    public ObjectA(String code) {
        this.code = code;
    }

    public String getCode() { return code; }
    public List<ObjectA> getSubList() { return subList; }
}

情况1:TreeMap存储所有节点(键为层级编码)

如果你的TreeMap是把所有节点的编码作为键(比如TreeMap<String, String>,值可以是节点的其他业务数据),那可以按以下步骤转换:

核心思路

  1. 先把所有节点转换成ObjectA实例,用一个Map缓存起来,方便快速查找父节点;
  2. 遍历每个节点,通过编码规则找到它的父节点(比如01-01的父编码是01);
  3. 将当前节点加入父节点的subList,没有父节点的就是根节点,直接加入结果列表。

完整代码示例

import java.util.*;

public class TreeMapConverter {
    public static void main(String[] args) {
        // 模拟你的TreeMap,键是节点编码,值可以是节点的业务名称
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("01", "父节点01");
        treeMap.put("01-01", "子节点01-01");
        treeMap.put("01-02", "子节点01-02");

        // 执行转换
        List<ObjectA> nestedList = convertToNestedObjectA(treeMap);

        // 打印验证结构
        printNestedStructure(nestedList, 0);
    }

    private static List<ObjectA> convertToNestedObjectA(TreeMap<String, String> treeMap) {
        Map<String, ObjectA> nodeCache = new HashMap<>();
        List<ObjectA> rootNodes = new ArrayList<>();

        // 第一步:创建所有ObjectA节点并缓存
        for (Map.Entry<String, String> entry : treeMap.entrySet()) {
            String code = entry.getKey();
            ObjectA node = new ObjectA(code);
            // 这里可以设置其他属性,比如node.setLabel(entry.getValue());
            nodeCache.put(code, node);
        }

        // 第二步:关联父子节点
        for (String code : treeMap.keySet()) {
            ObjectA currentNode = nodeCache.get(code);
            String parentCode = getParentCode(code);

            if (parentCode != null && nodeCache.containsKey(parentCode)) {
                // 找到父节点,加入子列表
                nodeCache.get(parentCode).getSubList().add(currentNode);
            } else {
                // 没有父节点,作为根节点
                rootNodes.add(currentNode);
            }
        }

        return rootNodes;
    }

    // 根据编码获取父节点编码,适配你的层级编码规则
    private static String getParentCode(String code) {
        int lastDashPos = code.lastIndexOf("-");
        return lastDashPos == -1 ? null : code.substring(0, lastDashPos);
    }

    // 辅助打印方法,直观展示嵌套结构
    private static void printNestedStructure(List<ObjectA> nodes, int indentLevel) {
        String indent = "  ".repeat(indentLevel);
        for (ObjectA node : nodes) {
            System.out.println(indent + node.getCode());
            printNestedStructure(node.getSubList(), indentLevel + 1);
        }
    }
}

情况2:TreeMap存储父节点到子节点列表的映射

如果你的TreeMap是直接存父节点和子节点的映射(比如TreeMap<String, List<String>>,键是父编码,值是子编码列表),那可以用递归的方式更简单地转换:

核心思路

递归遍历父节点,为每个父节点创建ObjectA,然后递归处理它的子节点,把子节点的ObjectA加入父节点的subList。

代码示例

import java.util.*;

public class ParentChildMapConverter {
    public static void main(String[] args) {
        TreeMap<String, List<String>> parentChildMap = new TreeMap<>();
        parentChildMap.put("01", Arrays.asList("01-01", "01-02"));
        // 如果子节点还有子节点,可以继续添加,比如parentChildMap.put("01-01", Arrays.asList("01-01-01"));

        // 创建根节点并填充子节点
        ObjectA root = new ObjectA("01");
        root.getSubList().addAll(buildChildNodes(parentChildMap, "01"));
        List<ObjectA> result = Collections.singletonList(root);

        // 打印验证
        printNestedStructure(result, 0);
    }

    private static List<ObjectA> buildChildNodes(TreeMap<String, List<String>> map, String parentCode) {
        List<ObjectA> childNodes = new ArrayList<>();
        if (!map.containsKey(parentCode)) {
            return childNodes;
        }
        for (String childCode : map.get(parentCode)) {
            ObjectA child = new ObjectA(childCode);
            // 递归处理当前子节点的子节点
            child.getSubList().addAll(buildChildNodes(map, childCode));
            childNodes.add(child);
        }
        return childNodes;
    }

    // 复用之前的printNestedStructure方法
    private static void printNestedStructure(List<ObjectA> nodes, int indentLevel) {
        String indent = "  ".repeat(indentLevel);
        for (ObjectA node : nodes) {
            System.out.println(indent + node.getCode());
            printNestedStructure(node.getSubList(), indentLevel + 1);
        }
    }
}

总结

不管你的TreeMap是哪种存储形式,只要能明确父子节点的关联规则,都可以轻松转换成你需要的嵌套ObjectA结构。如果你的编码规则或者TreeMap结构有特殊调整,只需要修改getParentCode方法或者递归逻辑即可。

内容的提问来源于stack exchange,提问作者Madan Thunderfist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:51:58