52

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.

newbie
  • 1,884
  • 7
  • 34
  • 55
  • 1
    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}`. – Prakash K Sep 12 '13 at 06:45
  • i tried doing this one but i throws me an error heres the stacktrace: `Property 'bool' not found on type java.util.HashMap$Entry` – newbie Sep 12 '13 at 07:01
  • can you post the code as to what you did. – Prakash K Sep 12 '13 at 07:54
  • So you just want to print the value for the key - `"key"` of your map? – Rohit Jain Sep 12 '13 at 08:09
  • use only the inner foreach and remove the outer foreach loop. Use this in items `items="${key['key']}"` where `${key}` is the map set in request attribute. – Prakash K Sep 12 '13 at 08:13

3 Answers3

97

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.

jason
  • 1,081
  • 9
  • 7
  • 4
    This should be the accepted answer, but note that quote marks for value are missing `` – Gonzalo Dec 04 '14 at 08:45
  • This 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 – MR AND Jun 26 '17 at 17:59
  • 1
    If `key` is type of `Integer` then you should get the value using `` (Anwser from: https://stackoverflow.com/a/924556/2594961 ) – yaylitzis Oct 05 '21 at 08:27
4

could you please try below code

<c:forEach var="hash" items="${map['key']}">
        <option><c:out value="${hash}"/></option>
  </c:forEach>
Gautam
  • 3,276
  • 4
  • 31
  • 53
-1

I had issue with the solutions mentioned above as specifying the string key would give me javax.el.PropertyNotFoundException. The code shown below worked for me. In this I used status to count the index of for each loop and displayed the value of index I am interested on

<c:forEach items="${requestScope.key}"  var="map" varStatus="status" >
    <c:if test="${status.index eq 1}">
        <option><c:out value=${map.value}/></option>
    </c:if>
</c:forEach>    
MR AND
  • 376
  • 7
  • 29