9 Replies Latest reply: May 15, 2008 11:58 PM by 807591 RSS

    How to iterate through multiple HashMaps inside one HashMap

    807591
      Hi,

      Can some one please tell me how can I iterate through a HashMap that may contain some more HashMaps and they inturn may contain more HashMaps and so on. How can I iterate throught all the HashMaps recursively in order i.e. First HashMap finds a HashMap iterate through that and if that finds another HashMap them iterate through that first and so on.

      Thanks
        • 1. Re: How to iterate through multiple HashMaps inside one HashMap
          807591
          Use recursion or a stack.
          • 2. Re: How to iterate through multiple HashMaps inside one HashMap
            807591
            @op, how do you know if the object you are retrieving is a hashmap
            or some other object? (or is that your question?)
            • 3. Re: How to iterate through multiple HashMaps inside one HashMap
              807591
              Demo:
              import java.util.*;
              
              public class MapOMatic extends HashMap < String, MapOMatic > {
                  private static final long serialVersionUID = 0;
              
                  public static void main(String[] args) {
                      MapOMatic map1 = new MapOMatic();
                      MapOMatic map2 = new MapOMatic();
                      MapOMatic map3 = new MapOMatic();
                      MapOMatic map4 = new MapOMatic();
              
                      map1.put("map2", map2);
                      map1.put("map3", map3);
                      map3.put("map4", map4);
                      dumpKeys(map1);
                  }
              
                  static void dumpKeys(MapOMatic map) {
                      if (!map.isEmpty()) {
                          System.out.println(map.keySet());
              
                          for(String key : map.keySet()) {
                              MapOMatic child = map.get(key);
                              if (child != null)
                                  dumpKeys(child);
                          }
                      }
                  }
              }
              • 4. Re: How to iterate through multiple HashMaps inside one HashMap
                807591
                Hi,

                Here is the sample scenario and the heirarchy can be deep x-number of levels:
                HaspMap hm11 = new HashMap();
                hm11.put("hm11","valuehm11");
                hm11.put("hm111","valuehm111");
                hm11.put("hm1111","valuehm1111");
                
                HaspMap hm10 = new HashMap();
                hm10.put("hm10","valuehm10");
                hm10.put("hm100",hm11);
                hm10.put("hm1000","valuehm1000");
                
                HaspMap hm9 = new HashMap();
                hm9.put("hm9",hm11);
                hm9.put("hm99","valuehm99");
                hm9.put("hm999",hm10);
                
                HaspMap hm8 = new HashMap();
                hm8.put("hm8","valuehm8");
                hm8.put("hm88",hm9);
                hm8.put("hm888","valuehm888");
                
                HaspMap hm7 = new HashMap();
                hm7.put("hm7","valuehm7");
                hm7.put("hm77",hm8);
                hm7.put("hm777","valuehm777");
                
                HaspMap hm6 = new HashMap();
                hm6.put("hm6",hm7);
                hm6.put("hm66","valuehm66");
                hm6.put("hm6666","valuehm666");
                
                HaspMap hm1 = new HashMap();
                hm1.put("hm1a",hm6);
                hm1.put("hm11a",hm7);
                hm1.put("hm111a","valuehm111");
                hm1.put("hm1","valuehm1")
                
                ////
                    for (Iterator j = h.keySet ().iterator () ; j.hasNext () ; )
                    {
                      Object obj2 = (Object) j.next () ;
                      Object objValue2 = (Object) h.get (obj2) ;
                      if (objValue2 instanceof java.util.HashMap)
                      {
                        //wants to loop this HashMap now before the top one
                      }
                      else
                      {
                         ;
                      }
                    }
                ///
                Want to find out how to loop through this.

                Thanks
                • 5. Re: How to iterate through multiple HashMaps inside one HashMap
                  796440
                  Want to find out how to loop through this.
                  What do you not understand?

                  Do you know how to do it for a single level?
                  • 6. Re: How to iterate through multiple HashMaps inside one HashMap
                    807591
                    Yes I know how to do a single level but somehow can't get to loop this structure. Any help is appreciated.

                    Thanks
                    • 7. Re: How to iterate through multiple HashMaps inside one HashMap
                      807591
                      Map-O-Matic


                      More seriously, use recursion. It's Man's best friend.
                      • 8. Re: How to iterate through multiple HashMaps inside one HashMap
                        807591
                        Do you know how to do recursion? It's easy by doing it this way (as suggested above). Please check out a tutorial on this. If you have any questions, we can help.
                        • 9. Re: How to iterate through multiple HashMaps inside one HashMap
                          807591
                          I tried the above code given,its working for me but i am not able to get the value of sub map.Here is my code
                          for (Iterator j = data.keySet ().iterator () ; j.hasNext () ; )
                                    {
                                    Object obj2 = (Object) j.next () ;
                                    Object objValue2 = (Object) data.get (obj2) ;
                                    if (objValue2 instanceof java.util.HashMap)
                                    {
                                    //wants to loop this HashMap now before the top one
                                         Iterator iter = ((Map) objValue2).keySet().iterator();
                                         String key = iter.next().toString();
                                         String value = this.objValue2.get(key).toString(); // Giving Error
                                    }
                                    }

                          Plz could you tell me how to get the value.
                          Please help me regarding this.


                          Thanks in advance