How to iterate an ArrayList inside a HashMap using JSTL?
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?
How to iterate ArrayList HashMap String, String in a jsp page using JSTL
There are few things you have to change:
ResultSet are by-default forward only, since your accessing column Stk_prdid
multiple time , it does not returns any value.
public ArrayList getAllStockDetails() {
...
Statement stmt = dcon.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
....
while (rs.next()) {
HashMap<String, String> stkMap = new HashMap<String, String>();//<-- move here
stkList.add(stkMap);
...
}
....
}
In jsp:
...
<c:forEach var="row" items="${stkList}">
...
<td>${row.prdStk}</td>
...
...
Iterating through an arraylist in a hashmap based on key in JSTL
Please try to use
$
instead if
#
Iterate the Hashmap/ArrayList using JSTL
Assuming your ${xml}
contains the array
attribute of request/session:
- You're trying to read the entries of the
Map
s directly. You should start reading the elements from the list that areMap
s. From How to iterate an ArrayList inside a HashMap using JSTL?, you're using the wrong syntax to access the current entry values of your map. You should use
entry.value
in your code:<table border="1">
<c:forEach items="${xml}" var="itemMap">
<c:forEach items="${itemMap}" var="mapEntry">
<tr>
<td>
<c:choose>
<c:when test="${mapEntry.key == 'name'}">
<c:out value="Name:" />
</c:when>
<c:when test="${mapEntry.key == 'catVal'}">
<c:out value="Cat value:" />
</c:when>
<c:when test="${mapEntry.key == 'catStat'}">
<c:out value="Cat status:" />
</c:when>
</c:choose>
</td>
<td>
${mapEntry.value}
</td>
</tr>
</c:forEach>
</c:forEach>
</table>
How to iterate over a list of objects which have a map as a field in jstl?
I think you have to iterate map (the field map
of the class Spravochnik
) too.
Maybe like that:
<c:forEach var="sprav" items="${spravList}">
<tr>
<td>
${sprav.id}
</td>
<td>
<c:forEach items="${sprav.map}" var="entry">
Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>
</td>
</tr>
</c:forEach>
Key and Value has ArrayList in Map by Using JSTL
I am not sure, but maybe you want something like this:
<%
Map<String, List<String>> myMap = new LinkedHashMap<String,List<String>>();
request.setAttribute("mainMenu", myMap);
List<String> adminItemsList = new ArrayList<String>();
adminItemsList.add("User Creation");
adminItemsList.add("Branch Creation");
List<String> lookupItemsList = new ArrayList<String>();
lookupItemsList.add("Country");
lookupItemsList.add("Language");
myMap.put("Administrator", adminItemsList);
myMap.put("Lookup Configuration", lookupItemsList);
%>
<c:forEach items="${mainMenu}" var="myMenu">
<li id="lookup" class="mail"><a href="#lookup">${myMenu.key}<span>26</span></a>
<ul class="sub-menu">
<c:forEach items="${myMenu.value}" var="item" varStatus="loop">
<li><a href="#"><em>02</em>_${item}_<span>14</span></a></li>
</c:forEach>
</ul>
</li>
</c:forEach>
out:
Administrator26
- 02_User Creation_14
- 02_Branch Creation_14
Lookup Configuration26
- 02_Country_14
- 02_Language_14
I used LinkedHashMap to remember order of keys i putted in Map.
How to iterate HashMap using JSTL forEach loop?
Try this,
suppose my MAP is :-
Map<String, String> countryList = new HashMap<String, String>();
countryList.put("United States", "Washington DC");
countryList.put("India", "Delhi");
countryList.put("Germany", "Berlin");
countryList.put("France", "Paris");
countryList.put("Italy", "Rome");
request.setAttribute("capitalList", countryList);
So in JSP ,
<c:forEach var="country" items="${capitalList}">
Country: ${country.key} - Capital: ${country.value}
</c:forEach>
Hope this helps !
Related Topics
Why Does the Jvm Consume Less Memory Than -Xms Specified
Java - Process.Destroy() Source Code for Linux
Gson - Convert from JSON to a Typed Arraylist<T>
Android/Java -- Post Simple Text to Facebook Wall
How to Downsample Images Within PDF File
Start a Jar File Like Service in Linux
Firestore Query - Checking If Username Already Exists
Place Cursor at the End of Text in Edittext
Background Service for Android Oreo
Why I Cant Use the Populateviewholder Override Method
Onclicklistener Not Responding
How to Troubleshoot "Inconsistency Detected: Dl-Lookup.C: 111" (Java Result 127) Error
Javafx Freeze on Desktop.Open(File), Desktop.Browse(Uri)
Ionic Build Android Error When Downloading Gradle
How to Use Vectordrawable in Buttons and Textviews Using Android:Drawableright
Android-Java- How to Sort a List of Objects by a Certain Value Within the Object