Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Creating Map [Map or String]

843793Apr 18 2010 — edited Apr 19 2010
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"}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 17 2010
Added on Apr 18 2010
1 comment
182 views