Iterating an ArrayList inside a HashMap in JSTL
In Java Server Pages (JSP), the Java Standard Tag Library (JSTL) provides convenient tags for iterating over data structures. This includes iterating over maps, where each key-value pair is stored as a Map.Entry object.
To iterate over both the keys and values of a HashMap, including any ArrayLists stored as values, you can use nested
<code class="jsp"><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:forEach items="${myMap}" var="entry"> <span>Key: ${entry.key}</span> <c:forEach items="${entry.value}" var="item" varStatus="loop"> <span>Value: ${item}</span> <c:if test="${!loop.last}">, </c:if> </c:forEach> <br> </c:forEach></code>
This code iterates over the myMap HashMap and, for each key-value pair, outputs the key followed by a list of values in the corresponding ArrayList. The varStatus attribute is used to determine whether the current item is the last in the list, so that a comma is added between items only if they are not the last.
Additional Notes:
The above is the detailed content of How to Iterate over an ArrayList within a HashMap in JSTL?. For more information, please follow other related articles on the PHP Chinese website!