I have a LinkedHashMap
which contains another LinkedHashMap
as below:
LinkedHashMap<String,LinkedHashMap<String,String>> containerMap = new LinkedHashMap<String,LinkedHashMap<String,String>>();
I want to extract the values from the container Map individually? I mean for each key of the container map I have a LinkedHashMap
I want to use that to display in my dropdown list on my JSP
Any ideas?
-
So you need to have multiple drop downs based on container?Syam S– Syam S07/18/2014 13:28:40Commented Jul 18, 2014 at 13:28
-
The LinkedHashMap contained in the container map contains the key and value of options that i want to display as a dropdown on my jsp.What i mean to say is Suppose the container map has <Q1,LinkedHashMap<String,String>> then Q1 is the key (question number) and its value (the linkedHashMap) contains the Value and description that i want to display on my JSP.JavaDeveloper– JavaDeveloper07/19/2014 09:04:44Commented Jul 19, 2014 at 9:04
-
We understand the contents of the maps. What we aren't clear on what it is that you are ultimately wanting ... a single drop down that has the contents of the inner LinkedHashMap based on some key selector that you pass in, or multiple drop downs (one for every key in the containerMap)?alfreema– alfreema07/23/2014 03:48:09Commented Jul 23, 2014 at 3:48
-
@alfreema its been resolved i just wanted the content of LHM contained in the container map to displayed on the jsp as a sropdown list. The only problem was the iteration which became a cup of cake with Map.Entry :)JavaDeveloper– JavaDeveloper07/23/2014 05:07:42Commented Jul 23, 2014 at 5:07
2 Answers 2
I assume you need multiple dropdowns based on container map. If so from you sevlet set the map to request object as request.setAttribute("containerMap", containerMap)
and the use nested forEach loop of jstl in jsp
<c:forEach items="${containerMap}" var="containerEntry" >
<select name="${containerEntry.key}" id="${containerEntry.key}">
<c:forEach items="${containerEntry.value}" var="innerEntry">
<option value="${innerEntry.key}">
<c:out value="${innerEntry.value}" />
</option>
</c:forEach>
</c:forEach>
-
Thanks for reply.But the problem is little more complicated. The LinkedHashMap contained in the container map contains the key and value of options that i want to display as a dropdown on my jsp.What i mean to say is Suppose the container map has Q1,LinkedHashMap then Q1 is the key (question number) and its value (the linkedHashMap) contains the Value and description that i want to display on my JSP.JavaDeveloper– JavaDeveloper07/19/2014 08:54:11Commented Jul 19, 2014 at 8:54
-
His solution does pretty much exactly what you are describing, unless you want only a single drop down that reflects some key that is being passed in.alfreema– alfreema07/23/2014 03:49:01Commented Jul 23, 2014 at 3:49
LinkedHashMap<String,LinkedHashMap<String,String>> containerMap = new LinkedHashMap<String,LinkedHashMap<String,String>>();
I omitted the types (assuming you are using at least Java 7)
LinkedHashMap<String, String> lhMap1 = new LinkedHashMap<>();
lhMap1.put("a", "b");
LinkedHashMap<String, String> lhMap2 = new LinkedHashMap<>();
containerMap.put("1", lhMap1);
containerMap.put("2", lhMap2);
Then on your container you can call get()
containerMap.get("1"); // will give you lhMap1
containerMap.get("1").get("a"); // will return 'b'
Also, keySet() and values() are useful.
containerMap.keySet(); // will give you a Set<String>
containerMap.values(); // will give you a Collection<LinkedHashMap<String, String>>
-
Thanks for the reply i am evaluating it :)JavaDeveloper– JavaDeveloper07/19/2014 08:58:47Commented Jul 19, 2014 at 8:58
-
Thanks somthing smiliar i did but i used Map.Entry thanks a lot :)JavaDeveloper– JavaDeveloper07/21/2014 11:21:24Commented Jul 21, 2014 at 11:21