Get value from hashmap based on key to JSTL
121,735
Solution 1
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.
Solution 2
could you please try below code
<c:forEach var="hash" items="${map['key']}">
<option><c:out value="${hash}"/></option>
</c:forEach>
Author by
newbie
Updated on July 09, 2022Comments
-
newbie almost 2 years
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 somethingUPDATE:
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. -
Gonzalo over 9 yearsThis should be the accepted answer, but note that quote marks for value are missing
<c:out value="${map['key']}"/>
-
MR AND almost 7 yearsThis is what my map looks like {key1=23, key2=1, key3=0}. I get this error while using the code above: javax.el.PropertyNotFoundException: Property 'key1' not found on type java.util.HashMap$Node
-
yaylitzis over 2 yearsIf
key
is type ofInteger
then you should get the value using<c:out value="${map[(1).intValue()]}"/>
(Anwser from: stackoverflow.com/a/924556/2594961 )