簡體   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