Browse HashMapInteger, HashMapInteger, ArrayListInteger

1

I have the following HashMap :

HashMap<Integer,HashMap<Integer,ArrayList<Integer>>> hm = new HashMap<>();

When I do this:

Set<Integer> keys = hm.keySet();
for(int i : keys) System.out.println(i + ": " +hm.get(i));

I have an output like this:

2: {2=[1, 2, 3, 4]}

How can I go through the HashMap principal, to have access to the keys and values of HashMap inner?

    
asked by anonymous 03.12.2014 / 12:31

1 answer

7

You should do this separately, ie go through the main and for each iteration go through the secondary.

code:

for(Map.Entry<Integer,HashMap<Integer,ArrayList<Integer>>> kv: hm.entrySet()){
      //percorre map principal
      System.out.println("Key: "+kv.getKey()); //chave do principal
      //busca o map inferior dessa key 
      HashMap<Integer,ArrayList<Integer>> secondmap= kv.getValue();
      //percorre o map inferior
      for(Map.Entry<Integer,ArrayList<Integer>> kvv: secondmap.entrySet() ){
           //faz o print da chave e de todos os valores do arraylist
           for (int valor : kvv.getValue()){                               
                  System.out.println("Key: "+kvv.getKey() + "value "+valor;
           }

      }

}
    
03.12.2014 / 12:37