-1

While parsing a JSON file something like this:

{
"heading1" : [
  {
    "heading2" : {
        "subhead2" : {
           "value" : "value<heading2>"
        }
     },
     "heading3" : [
        {
           "heading4" : {
              "subhead4" : "value<heading4>",
           },
           "heading5" : [
              {
                 "subhead51" : {
                    "511" : "value<heading51>",
                 },
                 "subhead52" : {
                    "522" : "value<heading52>",
                 },
               <..similar such heading5 sets>
              }
           ],
          }
      }
  }
 ],
}

I have an ArrayList variable as below.

   List<Map<String,String>> childMap = new ArrayList<Hashmap<String,String>>();

I'm getting values into this childMap like below:

  for(i = 0; i<result.length; i++){
    Map<String,String> childSubMap = new HashMap<String,String>();
     childsubMap.put(heading51,value<heading51>);
     childsubMap.put(heading52,value<heading52>);

    childMap.add(childSubMap);
  }

Parent Map variable:

   Map<String,String> parentMap = new HashMap<String,String>();
     parentMap.put(heading2,value<heading2>);
     parentMap.put(heading4,value<heading4>);

I'm using the final combined map for data extraction.

Is there a way possible to combine it like below:

                 Key             |       Value
  __________________________________________________________
  heading2, value<heading2>      |  <Set 1>
  heading4, value<heading4>      |    heading51, value<heading51>
                                 |    heading52, value<heading52>
                                 |  <Set 2>
                                 |    heading51, value<heading51>
                                 |    heading52, value<heading52>
                                 |  ..<many such Sets>

Thanks for the help in advance!

curlyreggie
  • 1,530
  • 4
  • 21
  • 31

1 Answers1

2

If all you want to do is merge all the maps in the list into a single map, then do

for (Map<String,String> m : childMap)
{
    parentMap.putAll(childMap);
}

If you have any duplicate keys, the last one will be kept and the rest discarded.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • Thank you sir! I tried with using a new Class variable, declare my required datatypes explicitly and extract data from them. This seems to be working fine. I'm still curious though. – curlyreggie May 09 '12 at 02:22