JSTL forEach Map Iteration
JSTL forEach Map Iteration Example explains how to iterating a map using JSTL Taglib
Map is an object that stores key / value pairs. You can find its value from a particular key. Keys must be unique, but values may be duplicated
Consider a JSP page, which contains a HashMap with lot of key value pairs inside it and we need to iterate and display the values inside that particular HashMap
You can access ${entry.key} and ${entry.value} from a Map using a variable var="entry"
This JSTL 'c:forEach is available for all the List / Collection implementations in java
Following example shows How to iterate a JSTL HashMap using JSTL c:forEach
Required Libraries
Project Structure
JSTL forEach HashMap Iteration
// Iterate HashMap With JSTL <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ page import="java.util.HashMap"%> <% HashMap<String,String> numMap = new HashMap<String,String>(); numMap.put("1","one"); numMap.put("2","two"); numMap.put("3","three"); request.setAttribute("numMap", numMap); %> <html> <body> <c:forEach items="${numMap}" var="entry"> ${entry.key})${entry.value}<br/> </c:forEach> </body> </html>
Output
1) one2) two
3) three