Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate an ArrayList inside a HashMap using JSTL?

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?

like image 577
Rakesh Juyal Avatar asked Jan 22 '10 13:01

Rakesh Juyal


People also ask

How do I iterate a Map in JSTL?

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.

How to iterate list of HashMap in JSP?

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. In case of maps, every iteration the var will give you a Map.

What is the syntax for forEach loop in JSTL?

The <c:for each > is an iteration tag used for repeating the nested body content for fixed number of times or over the collection. These tag used as a good alternative for embedding a Java while, do-while, or for loop via a scriptlet.


2 Answers

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:

  • How to loop through a HashMap in JSP?
  • Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern
  • How to loop over something a specified number of times in JSTL?
like image 117
BalusC Avatar answered Sep 23 '22 16:09

BalusC


Did you try something like this?

<c:forEach var='item' items='${map}'>     <c:forEach var='arrayItem' items='${item.value}' />       ...     </c:forEach> </c:forEach> 
like image 25
dcp Avatar answered Sep 20 '22 16:09

dcp