繁体   English   中英

将HashMap作为值添加到HashMap

[英]Adding a HashMap to a HashMap as a value

我正在尝试将键(字符串),值(HashMap)添加到另一个HashMap。 我不知何故继续在这里弄乱语法和逻辑。 我怎么做? 我在这里初始化了kmap,然后我想添加一个键,该键是字符串,而值是另一个HashMap<String, List<Integer>>

这些可以在以下参数中看到:

static HashMap<String, HashMap<String, List<Integer>>> kmap = new HashMap<String, HashMap<String, List<Integer>>>();

public synchronized static void AddMapToList_ofMAP_(HashMap<String, List<Integer>> value, String key) {

    if (!kmap.containsKey(key)) {
        kmap.put(key, new HashMap<String, List<Integer>>());
    }
    HashMap<String, List<Integer>> q = kmap.get(key);

    q.put(key, value);
}

在您的参数中,您有一个名为value的HashMap。 然后,您尝试将其添加到HashMap内的HashMap中,但是其中的值必须为整数列表。

固定:

static HashMap<String, HashMap<String, List<Integer>>> kmap = new HashMap<String, HashMap<String, List<Integer>>>();

public synchronized static void AddMapToList_ofMAP_(
       List<Integer> value, String key) {  

    if (!kmap.containsKey(key)) {
        kmap.put(key, new HashMap<String, List<Integer>>());
    }
        HashMap<String, List<Integer>> q = kmap.get(key);

        q.put(key, value);
    }

另外,一种更好的方法是使用对象。 我不确定代码和对象的放置方式如何工作。

我还看到您通过该键获取了HashMap,但是您也将该键放在了HashMap中(里面是一个),肯定在那里只能有1个HashMap。

简单比你使它看起来。 您不需要单独的方法,只需在kmap上调用put(key, value)方法即可。 假设您在名为value的变量中具有Map<String, List<Integer>> ,则为:

kmap.put(key, value);

就这样。 只需一行。

我不确定您要在这里实现什么目标。 这是您可能想要做的。 随时编写更多测试用例并优化代码。 但是,这将为您提供一个基础结构。

公共类Stackoverflow {

static HashMap<String, HashMap<String, List<Integer>>> kmap = new HashMap<String, HashMap<String, List<Integer>>>();

public synchronized static void addIntegerToKmap(String kmapKey, String intMapKey, Integer value) {
    if (!kmap.containsKey(kmapKey)) {
        Map<String, List<Integer>> intMap = new HashMap<String, List<Integer>>();

        HashMap<String, List<Integer>> stringListHashMap = new HashMap<String, List<Integer>>();
        List<Integer> integerList = new ArrayList<Integer>();

        integerList.add(value);
        stringListHashMap.put(intMapKey, integerList);
        kmap.put(kmapKey, stringListHashMap);
    }
    else {
        HashMap<String, List<Integer>> stringListHashMap = kmap.get(kmapKey);
        List<Integer> integerList = stringListHashMap.get(intMapKey);
        if (integerList != null && !integerList.isEmpty()) {
            integerList.add(value);
        }
        else {
            integerList = new ArrayList<Integer>();
            integerList.add(value);
            stringListHashMap.put(intMapKey, integerList);
        }
    }
}

public static void main(String args[]) {
    addIntegerToKmap("A", "A1", 1);
    addIntegerToKmap("A", "A1", 2);

    addIntegerToKmap("A", "A2", 12);
    addIntegerToKmap("A", "A2", 22);

    addIntegerToKmap("B", "B1", 1);

    addIntegerToKmap("A", "A1", 3);
}

}

逻辑尚不清楚,但也许您想要这个

q.put(key, value.get(key));

代替这个:

q.put(key, value);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM