猿问

设置嵌套在地图中的地图的值

在我的一个项目中,我尝试在传递给参数的嵌套地图中设置值并返回更新的地图。问题是:假设我不知道地图结构,如何在嵌套地图的给定路径中设置值?


我试图做到这一点。我试图递归调用set方法但无济于事,该方法没有返回 {age=1, human={lives=3, deaths=2}},而是返回 {deaths=2} 或null. 但是,请注意,这是我无数次尝试中的一次。


这是我的一种方法(其他方法已删除):


@SuppressWarnings("unchecked")

    private static Map<Object, Object> setNested(YamlParser parser, List<String> paths, String key, Object value, Map<Object, Object> previousMap, int loops) {

        Object found = parser.getObject(paths);

        if (!(found instanceof Map))

            return previousMap; // path is not nested

        Map<Object, Object> theMap = (Map<Object, Object>) found;

        theMap.put(key, value);

        // .... ?

        System.out.println(theMap);


        return setNested(parser, paths, key, theMap, theMap, loops + 1);

    }


杨__羊羊
浏览 156回答 2
2回答

慕码人8056858

添加所有缺失的中间映射的版本更简单:&nbsp; &nbsp; private static Map<String, Object> setNested(Map<String, Object> map, List<String> keys, Object value) {&nbsp; &nbsp; &nbsp; String key = keys.get(0);&nbsp; &nbsp; &nbsp; List<String> nextKeys = keys.subList(1, keys.size());&nbsp; &nbsp; &nbsp; Object newValue;&nbsp; &nbsp; &nbsp; if (nextKeys.size() == 0) {&nbsp; &nbsp; &nbsp; &nbsp; newValue = value;&nbsp; &nbsp; &nbsp; } else if (!map.containsKey(key)) {&nbsp; &nbsp; &nbsp; &nbsp; newValue = setNested(new LinkedHashMap<>(), nextKeys, value);&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; newValue = setNested((Map<String, Object>) map.get(key), nextKeys, value);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; Map<String, Object> copyMap = new LinkedHashMap<>(map);&nbsp; &nbsp; &nbsp; copyMap.put(key, newValue);&nbsp; &nbsp; &nbsp; return copyMap;&nbsp; &nbsp; }

斯蒂芬大帝

我看不出YamlParser这个例子有什么用,我也不知道你到底想做什么。我认为,这是关于制作一个新地图,其中复制了中间地图和最终(叶)地图,并且新的叶子地图具有新值。如果这不是您所需要的,您可以随意修改它。也许它会提示您如何实现自己的方法:public class Test {&nbsp; &nbsp; &nbsp; private static Map<String, Object> setNested(Map<String, Object> map, List<String> keys, Object value) {&nbsp; &nbsp; &nbsp; &nbsp; String key = keys.get(0);&nbsp; &nbsp; &nbsp; &nbsp; List<String> nextKeys = keys.subList(1, keys.size());&nbsp; &nbsp; &nbsp; &nbsp; if (nextKeys.size() == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, Object> copyMap = new LinkedHashMap<>((Map) map);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; copyMap.put(key, value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return copyMap;&nbsp; &nbsp; &nbsp; &nbsp; } else if (!map.containsKey(key)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return map;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, Object> copyMap = new LinkedHashMap<>((Map) map);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, Object> nextMap = (Map<String, Object>) map.get(key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; copyMap.put(key, setNested(nextMap, nextKeys, value));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return copyMap;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Object> map1 = new LinkedHashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Object> map2 = new LinkedHashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; map2.put("lives", 3);&nbsp; &nbsp; &nbsp; &nbsp; map2.put("deaths", 2);&nbsp; &nbsp; &nbsp; &nbsp; map1.put("age", 1);&nbsp; &nbsp; &nbsp; &nbsp; map1.put("human", map2);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(map1);&nbsp; &nbsp; &nbsp; &nbsp; map1 = setNested(map1, Arrays.asList("human", "deaths"), 7);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(map1);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }注意:此方法可以在最低级别的映射中插入新键,但不能在中间映射中插入。
随时随地看视频慕课网APP

相关分类

Java
我要回答