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,7483 gold badges34 silver badges50 bronze badges
asked Jul 18, 2014 at 13:16
4
  • So you need to have multiple drop downs based on container? Commented 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. Commented 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)? Commented 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 :) Commented Jul 23, 2014 at 5:07

2 Answers 2

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>
answered Jul 18, 2014 at 13:37
2
  • 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. Commented 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. Commented Jul 23, 2014 at 3: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>>
answered Jul 18, 2014 at 13:24
2
  • Thanks for the reply i am evaluating it :) Commented Jul 19, 2014 at 8:58
  • Thanks somthing smiliar i did but i used Map.Entry thanks a lot :) Commented Jul 21, 2014 at 11:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.