Forum Stats

  • 3,760,215 Users
  • 2,251,665 Discussions


Creating Map [Map or String]

843793 Member Posts: 41,732 Green Ribbon
edited Apr 19, 2010 2:24AM in Generics
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<em>);<br />
				if (lvar == null)<br />
					lvar = new HashMap<String, Object>();<br />
				prev.put(elements[i], lvar);<br />
				prev = lvar;<br />
			}<br />
			prev.put(elements[elements.length - 1], "ABC");<br />
		}<br />
<br />
	<pre class="jive-pre"><code class="jive-code">ouput : {tables={table={filed{name=ABC}}}, schema="ABC"}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            


This discussion has been closed.