1 Reply Latest reply: Apr 19, 2010 1:24 AM by 791266 RSS

    Creating  Map [Map or  String]

    843793
      Hi,
      please apologize if this question is already exists I searched in Generics section but I couldn't get it.

      My requirement is to create a Map of Map structure using the property, the last element in the Map of Map .. will be String.
      I have a property "tables_table_field_name" and value as "ABC", then I need to convert this property into Map of Map structure, like
      {tables={table={filed={name="ABC"}}}}. Property level might vary for instance, in the above property we have four elements[tables,table,field,name], in another property I could get more or less than this element size.

      I could do this easily in raw Map but coming to the Generic it became little bit tricky.

      here is the sample code I have written using minimal generic knowledge, Can any body guide me to follow the exact generic syntax.
                String[] properties = { "tables_table_field_name", "schema" };
                HashMap<String, Object> map = new HashMap<String, Object>();
                for (String property : properties) {
                     String[] elements = property.split("_");
                     Map<String, Object> prev = map;
                     for (int i = 0; i < elements.length - 1; i++) {
                          Map<String, Object> lvar = (HashMap) prev.get(elements);
                          if (lvar == null)
                               lvar = new HashMap<String, Object>();
                          prev.put(elements[i], lvar);
                          prev = lvar;
                     }
                     prev.put(elements[elements.length - 1], "ABC");
                }

           
      ouput : {tables={table={filed{name=ABC}}}, schema="ABC"}