In Java, a HashMap is a data structure that stores key-value pairs, where the keys are unique identifiers for the values. Often, it's necessary to iterate through both the HashMap and the ArrayLists stored within its values.
To do this using JSTL, we can employ the
Iterating over a HashMap
For a HashMap, the
<code class="java"><c:forEach items="${myMap}" var="entry"> Key = ${entry.key}<br> Value = ${entry.value}<br> </c:forEach></code>
Iterating over an ArrayList within a HashMap
Since the values in the HashMap are ArrayLists, we need to iterate over them within the outer loop.
<code class="java"><c:forEach items="${myMap}" var="entry"> Key = ${entry.key}<br> Values = <c:forEach items="${entry.value}" var="item"> ${item} ${!loop.last ? ', ' : ''}<br> </c:forEach><br> </c:forEach></code>
In this example, the inner loop uses the
Additional Resources
The above is the detailed content of How to Iterate over a HashMap and ArrayLists within it using JSTL. For more information, please follow other related articles on the PHP Chinese website!