dedpo
dedpo

Reputation: 502

Adding a HashMap to a HashMap as a value

I am trying to add a Key(String), Value(HashMap) to another HashMap. I somehow keep jumbling up the syntax and logic here. How do I do that? I have a kmap here initialized and then I want to add a key which is a string, and a value which is another HashMap<String, List<Integer>>)

These can be seen in the parameters below:

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);
}

Upvotes: 1

Views: 75

Answers (4)

Nuwan
Nuwan

Reputation: 31

I'm not sure what exactly you are trying to achieve here. Here's what possibly you may want to do. Feel free to write more test cases and optimize the code. However this will give you a based structure to work on.

public class 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);
}

}

Upvotes: 1

Erick G. Hagstrom
Erick G. Hagstrom

Reputation: 4945

It's WAY simpler than you're making it seem. You don't need a separate method, just invoke the put(key, value) method on your kmap. Assuming you have a Map<String, List<Integer>> in a variable named value, it's just:

kmap.put(key, value);

That's all. Just one line.

Upvotes: 1

Youssef Lahoud
Youssef Lahoud

Reputation: 529

The logic is not clear but maybe you want this

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

instead of this:

q.put(key, value);

Upvotes: -1

Walshy
Walshy

Reputation: 850

In your parameters you have got a HashMap called value. You are then trying to add that to the HashMap inside the HashMap but the value in that needs to be a List of integers.

Fix:

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);
    }

Also, a possible way to make this better is using an Object. I'm not sure how the code and what your putting in but an object could work.

I'm also seeing you get the HashMap by the key but you also put that key in the HashMap (The one inside), surely you could just have 1 HashMap there.

Upvotes: 1

Related Questions