0

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?

Anton Sarov
  • 3,712
  • 3
  • 31
  • 48
JavaDeveloper
  • 92
  • 1
  • 1
  • 11
  • So you need to have multiple drop downs based on container? – Syam S Jul 18 '14 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 > 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 Jul 19 '14 at 09: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 Jul 23 '14 at 03: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 Jul 23 '14 at 05:07

2 Answers2

1

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>
Community
  • 1
  • 1
Syam S
  • 8,421
  • 1
  • 26
  • 36
  • 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 Jul 19 '14 at 08: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 Jul 23 '14 at 03:49
0
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>>
Anton Sarov
  • 3,712
  • 3
  • 31
  • 48