I'm totally at loss. Here is my code
HashMap>> themap = new HashMap>>(); HashMap> outerMap = new HashMap>();
...
//themap is populated through OnCreate(), and works fine.
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
String currentfragment = "my_string"
...
HashMap<String, HashMap<String, HashMap<String, Integer>>> copymap = copy(themap);
System.out.println("1" + themap);
outerMap.putAll(copymap.get(currentfragment));
System.out.println("2" + themap);
...
}
In another method, some values of themap
are changed. The problem is that when onNavigationItemSelected is called, the first .println()
prints the right map, with the values changed, and the second .println()
prints themap
with its starting values, before they were changed. What did I do wrong?
EDIT: I've followed the instructions of How to copy HashMap (not shallow copy) in Java , but it still doesn't work. I've updated my previous code, and here is my copy
method:
public static HashMap<String, HashMap<String, HashMap<String, Integer>>> copy(HashMap<String, HashMap<String, HashMap<String, Integer>>> original) {
HashMap<String, HashMap<String, HashMap<String, Integer>>> copy = new HashMap<>();
for (Map.Entry<String, HashMap<String, HashMap<String, Integer>>> entry : original.entrySet()) {
HashMap<String, HashMap<String, Integer>> copyInnerMap = new HashMap<>();
for (Map.Entry<String, HashMap<String, Integer>> innerEntry : entry.getValue().entrySet()) {
HashMap<String, Integer> innerInnerMap = innerEntry.getValue();
HashMap<String, Integer> copiedInnerInnerMap = new HashMap<>();
for (Map.Entry<String, Integer> innerInnerMapEntry : innerInnerMap.entrySet()) {
copiedInnerInnerMap.put(innerInnerMapEntry.getKey(), innerInnerMapEntry.getValue());
}
copyInnerMap.put(innerEntry.getKey(), copiedInnerInnerMap);
}
copy.put(entry.getKey(), copyInnerMap);
}
return copy;
}
EDIT2: I've updated my copy method so that it's deep enough.
Here is the first output:
1{compu={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day2={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day3={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}}, chem={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day2={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day3={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}}, math={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day2={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=2}, Day3={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}}, physic={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day2={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day3={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}}, None={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day2={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=2}, Day3={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}}}
And the second:
2{compu={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day2={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day3={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}}, chem={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day2={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day3={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}}, math={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day2={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day3={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}}, physic={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day2={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day3={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}}, None={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day2={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}, Day3={Work hours=0, Study hours=0, Day grades=-1, Sheets filled=0, Fun=0}}}