Here is what you can do to reset a nested HashMap. The value is removed on line 22 and then added back as a new inner hashmap instance. Again, I loop through containingMap and the innerMap getting each map. Once I have a value to reset I call the reset function.
import java.util.*;
import java.lang.*;
public class Collections {
public HashMap<String, HashMap<String, Integer>>
createMap(String beta, String alpha, int m, HashMap<String,
Integer> innerStructure, HashMap<String, HashMap<String,
Integer>> containingStructure) {
while(m>0) {
innerStructure.put(beta, m);
m--;
}
containingStructure.put(alpha, innerStructure);
return containingStructure;
}
public void reset(HashMap<String, HashMap<String, Integer>>
map, int x) {
HashMap<String, Integer> betaMap = new HashMap<String, Integer>();
for(Map.Entry<String,HashMap<String,Integer>> entry: map.entrySet()) {
System.out.println("Key before is:" + entry.getKey());
if(entry.getValue() instanceof Map) {
for(Map.Entry<String, Integer> mapEntry: entry.getValue().entrySet()) {
if(mapEntry.getValue() == x) {
entry.getValue().remove(x);
map.put(entry.getKey(), betaMap);
}
}
}
}
}
public void print(HashMap<String, HashMap<String, Integer>> map) {
for(Map.Entry<String,HashMap<String,Integer>> entry: map.entrySet()) {
System.out.println("Key is:" + entry.getKey());
if(entry.getValue() instanceof Map) {
for(Map.Entry<String, Integer> mapEntry: entry.getValue().entrySet()) {
System.out.println(mapEntry.getKey());
}
}
}
}
public static void main(String[] args) {
Collections collections = new Collections();
HashMap<String, Integer> innerStructure = new HashMap<>();
HashMap<String, HashMap<String, Integer>> containingStructure = new HashMap<>();
containingStructure = collections.createMap("B1", "A1", 4, innerStructure, containingStructure);
collections.reset(containingStructure, 2);
collections.print(containingStructure);
}
}
It really doesn't change the Beta with put it is a matter of evaluating each entry and making sure it is a new HashMap type being used in it's place. I think this should help.