I have this problem where I tried to create a 2D HashMap by nesting one of them inside another but I have this weird issue where some of the data gets overridden by more data and I’m not really sure how to fix it. Many thanks for all your time and effort in advance.
import java.util.HashMap;
public static void main(String args[]) {
HashMap<Integer, HashMap<Integer, Integer>> outerMap = new HashMap<Integer, HashMap<Integer, Integer>>();
HashMap<Integer, Integer> innnerMap = new HashMap<Integer, Integer>();
int number;
innnerMap.put(5, 100);
outerMap.put(6, innnerMap);
innnerMap.put(5, 77);
outerMap.put(10, innnerMap);
innnerMap.put(33, 88);
outerMap.put(6, innnerMap);
System.out.println(outerMap.get(6).get(5));
}
}
The output this code gives is 77 even though it’s clear that 100 is the intended output. I still say dark magic is to blame here :/
EDIT: My post is completely different to the one I’m marked as a duplicate to, my post is about nested HashMaps and there’s is about list and static fields.
This post has been answered but it’s not a duplicate!