First, I don't think it is a good idea to have a HashMap
as a key
in another HashMap
. It looks like you are representing a map of key value pairs (arrival/destination?), you would be better off creating an class just for that and adding that class to a HashMap
. Using your current structure will cause more confusion than benefits.
That said, it is possible to loop through this.
One way to loop through a HashMap is to loop trhough its entry set. So if h
is a HashMap<Integer, Integer>
, you loop as:
for(Map.Entry<Integer, Integer> entry : h.entrySet)
{
...
}
So, for a HashMap<HashMap<Integer, Integer>, HashMap<Integer, Integer>>
called last
for(Map.Entry<HashMap<Integer, Integer>, HashMap<Integer, Integer>> entry : last.entrySet())
{
HashMap<Integer, Integer> key = entry.getKey();
for(Map.Entry<Integer, Integer> keyMapEntry : key.entrySet())
{
//Loop through the value HashMap.
//keyMapEntry.getKey() gives the key of the Hashmap that is the key in the
//original Hashmap, keyMapEntry.getValue() gives the value
}
HashMap<Integer, Integer> value = entry.getValue();
for(Map.Entry<Integer, Integer> valueMapEntry : value.entrySet())
{
//Loop through the key HashMap
//valueMapEntry.getKey() gives the key of the Hashmap that is the value in the
//original Hashmap, valueMapEntry.getValue() gives the value
}
}