I want to get the value of HashMap
based on key.
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>(); ArrayList<String> arrayList = new ArrayList<String>(); map.put("key", arrayList); request.setAttribute("key", map);
What i did is
<c:forEach var="map" items="${requestScope.key}"> <c:forEach var="hash" items="${map.value}"> <option><c:out value="${hash}"/></option> </c:forEach> </c:forEach>
But it seems it's printing everything, what i want to do is to get the value depends on key like: hash.key
or something
UPDATE:
I did something like this but it still doesn't work
<c:forEach var="map" items="${requestScope.key}"> <c:forEach var="hash" items="${map['key']}"> <option><c:out value="${hash}"/></option> </c:forEach> </c:forEach>
and the StackTrace: Property 'External' not found on type java.util.HashMap$Entry
I'm pretty sure that there is really that kind of key.
You can use ${map["key_name"]} where key_name is the string key i.e. map. put("key_name", value) and you can access the key simply as ${map. key} .
You can use the same technique to loop over a HashMap in JSP which we have used earlier to loop over a list in JSP. The JSTL foreach tag has special support for looping over Map, it provides you both key and value by using var attribute. In the case of HashMap, object exported using var contains Map. Entry object.
if all you're trying to do is get the value of a single entry in a map, there's no need to loop over any collection at all. simplifying gautum's response slightly, you can get the value of a named map entry as follows:
<c:out value="${map['key']}"/>
where 'map' is the collection and 'key' is the string key for which you're trying to extract the value.
could you please try below code
<c:forEach var="hash" items="${map['key']}"> <option><c:out value="${hash}"/></option> </c:forEach>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With