Home >Java >javaTutorial >How to Iterate over an ArrayList within a HashMap in JSTL?
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"> <foreach items="${myMap}" var="entry"> <span>Key: ${entry.key}</span> <foreach items="${entry.value}" var="item" varstatus="loop"> <span>Value: ${item}</span> <if test="${!loop.last}">, </if> </foreach> <br> </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!