【问题标题】:Restoring a value tree from its flat map representation从平面图表示中恢复值树
【发布时间】:2020-10-26 23:45:12
【问题描述】:

我有一个Map<String, String> 类型的平面地图,我想从中恢复原始值树Map<String, Object>。树的元素可以是Map<String, Object>,也可以是List<Object>,或者只是String。树的深度没有限制。例如:

public static void main(String[] args) {
    TreeMap<String, String> flatMap = new TreeMap<String, String>() {{
        put("1999.3:1", "23");
        put("1999.3:2", "24");
        put("1999.3:3", "25");
        put("1999.4:1", "1");
        put("1999.4:2", "2");
        put("1999.4:3.10", "42");
        put("2001.11.7:1", "23");
        put("2001.11.7:2", "24");
        put("2001.11.7:3", "25");
        put("2001.11.9:1", "1");
        put("2001.11.9:2", "2");
        put("2001.11.9:3", "3");
        put("2001.12", "45");
    }};
    System.out.println(flatMap);
}

平面地图:

{1999.3:1=23, 1999.3:2=24, 1999.3:3=25, 1999.4:1=1, 1999.4:2=2, 1999.4:3.10=42,
 2001.11.7:1=23, 2001.11.7:2=24, 2001.11.7:3=25,
 2001.11.9:1=1, 2001.11.9:2=2, 2001.11.9:3=3, 2001.12=45}

原始值树:

{1999={3=[23, 24, 25], 4=[1, 2, {10=42}]},
 2001={11={7=[23, 24, 25], 9=[1, 2, 3]}, 12=45}}

对面:Flatten nested Map containing with unknown level of nested Arrays and Maps recursively.

【问题讨论】:

    标签: java list tree treemap flatten


    【解决方案1】:

    遍历平面映射键并恢复原始树。对于每个键从左到右查找分隔符:

    • . - 嵌套对象是一张地图。下一个数字是地图中的键。
    • 冒号: - 嵌套对象是一个列表。下一个数字是列表中的索引(从 1 开始)。

    然后递归处理嵌套对象,通过平面图的键向右移动:

    public static void main(String[] args) {
        TreeMap<String, String> flatMap = new TreeMap<String, String>() {{
            put("1999.3:1", "23");
            put("1999.3:2", "24");
            put("1999.3:3", "25");
            put("1999.4:1", "1");
            put("1999.4:2", "2");
            put("1999.4:3.10", "42");
            put("2001.11.7:1", "23");
            put("2001.11.7:2", "24");
            put("2001.11.7:3", "25");
            put("2001.11.9:1", "1");
            put("2001.11.9:2", "2");
            put("2001.11.9:3", "3");
            put("2001.12", "45");
        }};
        TreeMap<String, Object> originalMap = new TreeMap<>();
        flatMap.forEach((key, value) -> processMap(key, value, originalMap));
    
        System.out.println(flatMap);
        System.out.println(originalMap);
    }
    
    @SuppressWarnings("unchecked")
    private static void processMap(String flatMapKey,
                                   String flatMapValue,
                                   Map<String, Object> map) {
        int dot = flatMapKey.indexOf('.');
        int colon = flatMapKey.indexOf(':');
    
        // nested map
        if (dot > -1 && (dot < colon || colon == -1)) {
            String key = flatMapKey.substring(0, dot);
            Object nestedMap = map.get(key);
            if (nestedMap == null) {
                map.put(key, new TreeMap<>());
            }
            nestedMap = map.get(key);
            processMap(flatMapKey.substring(dot + 1),
                    flatMapValue, (Map<String, Object>) nestedMap);
        }
        // nested list
        else if (colon > -1 && (colon < dot || dot == -1)) {
            String key = flatMapKey.substring(0, colon);
            Object nestedList = map.get(key);
            if (nestedList == null) {
                map.put(key, new ArrayList<>());
            }
            nestedList = map.get(key);
            processList(flatMapKey.substring(colon + 1),
                    flatMapValue, (List<Object>) nestedList);
        }
        // insert value
        else if (dot == colon) {
            map.put(flatMapKey, flatMapValue);
        }
    }
    
    @SuppressWarnings("unchecked")
    private static void processList(String flatMapKey,
                                    String flatMapValue,
                                    List<Object> list) {
        int dot = flatMapKey.indexOf('.');
        int colon = flatMapKey.indexOf(':');
    
        // nested map
        if (dot > -1 && (dot < colon || colon == -1)) {
            int index = Integer.parseInt(flatMapKey.substring(0, dot));
            while (list.size() < index) {
                list.add(null);
            }
            Object nestedMap = list.get(index - 1);
            if (nestedMap == null) {
                list.set(index - 1, new TreeMap<>());
            }
            nestedMap = list.get(index - 1);
            processMap(flatMapKey.substring(dot + 1),
                    flatMapValue, (Map<String, Object>) nestedMap);
        }
        // nested list
        else if (colon > -1 && (colon < dot || dot == -1)) {
            int index = Integer.parseInt(flatMapKey.substring(0, colon));
            while (list.size() < index) {
                list.add(null);
            }
            Object nestedList = list.get(index - 1);
            if (nestedList == null) {
                list.set(index - 1, new ArrayList<>());
            }
            nestedList = list.get(index - 1);
            processList(flatMapKey.substring(colon + 1),
                    flatMapValue, (List<Object>) nestedList);
        }
        // insert value
        else if (dot == colon) {
            int index = Integer.parseInt(flatMapKey);
            while (list.size() < index) {
                list.add(null);
            }
            list.set(index - 1, flatMapValue);
        }
    }
    

    平面地图:

    {1999.3:1=23, 1999.3:2=24, 1999.3:3=25, 1999.4:1=1, 1999.4:2=2, 1999.4:3.10=42,
     2001.11.7:1=23, 2001.11.7:2=24, 2001.11.7:3=25,
     2001.11.9:1=1, 2001.11.9:2=2, 2001.11.9:3=3, 2001.12=45}
    

    原始树:

    {1999={3=[23, 24, 25], 4=[1, 2, {10=42}]},
     2001={11={7=[23, 24, 25], 9=[1, 2, 3]}, 12=45}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-30
      • 1970-01-01
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      • 2018-07-23
      • 2020-01-12
      • 1970-01-01
      相关资源
      最近更新 更多