35

I have a map like this,

Map<Integer,ArrayList<Object>> myMap = new LinkedHashMap<Integer,ArrayList<Object>>();

Now I have to iterate this Map and then the ArrayList inside the map. How can I do this using JSTL?

informatik01
  • 16,038
  • 10
  • 74
  • 104
Rakesh Juyal
  • 35,919
  • 68
  • 173
  • 214

4 Answers4

77

You can use JSTL <c:forEach> tag to iterate over arrays, collections and maps.

In case of arrays and collections, every iteration the var will give you just the currently iterated item right away.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:forEach items="${collectionOrArray}" var="item">
    Item = ${item}<br>
</c:forEach>

In case of maps, every iteration the var will give you a Map.Entry object which in turn has getKey() and getValue() methods.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:forEach items="${map}" var="entry">
    Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>

In your particular case, the ${entry.value} is actually a List, thus you need to iterate over it as well:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:forEach items="${map}" var="entry">
    Key = ${entry.key}, values = 
    <c:forEach items="${entry.value}" var="item" varStatus="loop">
        ${item} ${!loop.last ? ', ' : ''}
    </c:forEach><br>
</c:forEach>

The varStatus is there just for convenience ;)

To understand better what's all going on here, here's a plain Java translation:

for (Entry<String, List<Object>> entry : map.entrySet()) {
    out.print("Key = " + entry.getKey() + ", values = ");
    for (Iterator<Object> iter = entry.getValue().iterator(); iter.hasNext();) {
        Object item = iter.next();
        out.print(item + (iter.hasNext() ? ", " : ""));
    }
    out.println();
}

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 6
    i like the way u used '${!loop.last ? ', ' : ''}' :D – Rakesh Juyal Jan 24 '10 at 05:24
  • 3
    You can find more methods here: http://java.sun.com/products/jsp/jstl/1.1/docs/api/javax/servlet/jsp/jstl/core/LoopTagStatus.html The `${loop.index}`, `${loop.first}` and `${loop.last}` are most useful here. – BalusC Jan 24 '10 at 05:47
  • yay ! Thanks for this post. This matches exactly my case which uses map.value as a list :) – Dzung Nguyen Oct 14 '11 at 15:38
  • should be terminated by – MrRaymondLee Nov 15 '13 at 17:00
  • Elvis has left the building ?: – Edward J Beckett Feb 04 '14 at 11:46
  • @BalusC I also have similar question [here](http://stackoverflow.com/questions/22293665/how-to-show-multiple-tables-depending-on-key-in-the-map-using-jstl) on the JSTL stuff. See if you can help me out.. Any help will be appreciated. –  Mar 10 '14 at 07:32
6

Did you try something like this?

<c:forEach var='item' items='${map}'>
    <c:forEach var='arrayItem' items='${item.value}' />
      ...
    </c:forEach>
</c:forEach>
dcp
  • 54,410
  • 22
  • 144
  • 164
5

you haven't closed c tag.try out this

 <c:forEach items="${logMap}" var="entry">
        Key = ${entry.key}, values = 
        <c:forEach items="${entry.value}" var="item" varStatus="loop">
            ${item} ${!loop.last ? ', ' : ''}
        </c:forEach><br>
    </c:forEach>
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
greeshma john
  • 51
  • 1
  • 1
0

You can also loop round just the map.value its self if you know the key

<c:forEach var="value" items="${myMap[myObject.someInteger]}">
    ${value}
</c:forEach>
gordon m
  • 11
  • 2