Home > Java > javaTutorial > body text

How to Iterate an ArrayList Inside a HashMap Using JSTL?

Susan Sarandon
Release: 2024-10-24 16:51:02
Original
860 people have browsed it

How to Iterate an ArrayList Inside a HashMap Using JSTL?

Iterating an ArrayList Inside a HashMap Using JSTL

In web development, JSTL (JavaServer Pages Standard Tag Library) provides a set of tags for simplifying common tasks in JSP (JavaServer Pages). One such task is iterating over data structures.

To iterate over a HashMap and the ArrayLists contained within it, you can use JSTL's tag. It enables looping through collections and maps:

For arrays and collections, var gives you the currently iterated item.

<code class="html"><c:forEach items="${collectionOrArray}" var="item">
    Item = ${item}<br>
</c:forEach></code>
Copy after login

For maps, var gives you a Map.Entry object, which has getKey() and getValue() methods.

<code class="html"><c:forEach items="${map}" var="entry">
    Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach></code>
Copy after login

Since the entry.value is a list, iterate over it as well:

<code class="html"><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></code>
Copy after login

The varStatus attribute enhances readability by tracking the loop's iteration status.

A similar Java implementation below helps understand the process:

<code class="java">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();
}</code>
Copy after login

For further reference, review the following resources:

  • [Looping through HashMap in JSP](https://stackoverflow.com/questions/11085751/how-to-loop-through-a-hashmap-in-jsp)
  • [Displaying JDBC ResultSet in JSP using MVC and DAO](https://stackoverflow.com/questions/23612802/show-jdbc-resultset-in-html-in-jsp-page-using-mvc-and-dao-pattern)
  • [Looping a specified number of times in JSTL](https://stackoverflow.com/questions/1054242/how-to-loop-over-something-a-specified-number-of-times-in-jstl)

The above is the detailed content of How to Iterate an ArrayList Inside a HashMap Using JSTL?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template