How do I use for-each to iterate generic collections?

In this example you will learn you to iterate a generic collection using the for-each loop. There was actually no for-each keyword or statement in Java. It just a special syntax of the for loop. The structure or syntax of the for-each loop is as follow.

for (type var : collections) {

This form of for loop is always read as foreach and the colon (:) character is read as “in”. Given that definition, if the type is a String you will read the syntax above as “foreach String var in collections”. The var in the statement above will be given each value from the collections during the iteration process.

Let’s compare two codes, the first one that use a generic and another code that does not use a generic so we can see the difference from the iteration perspective when working with a collection.

package org.kodejava.example.generics;

import java.util.ArrayList;
import java.util.List;

public class ForEachGeneric {

    public void loopWithoutGeneric() {
        List customers = new ArrayList();
        customers.add(new Customer());
        customers.add(new Customer());
        customers.add(new Customer());

        for (int i = 0; i < customers.size(); i++) {
            Customer customer = (Customer) customers.get(i);

    public void loopWithGeneric() {
        List<Customer> customers = new ArrayList<>();
        customers.add(new Customer());
        customers.add(new Customer());
        customers.add(new Customer());

        for (Customer customer : customers) {

What you see from the code snippet above is how much more clean our code is when we are using the generic version of the collection. In the first method, the loopWithoutGeneric we have to manually cast the object back to the Customer type. But in the second method, the loopWithGeneric method, no cast is needed as the collection will return the same type as what the collection was declared to hold.

How do use c:forEach JSTL tag?

The c:forEach tag in the core JSTL tag library is a useful tag when we want to iterate over a collection of data such as array. It is commonly use to render a tabular data in our web pages in form of HTML table.

In the example below we display a weather data that we stored as two dimensional array of string. After declaring and initializing the data with some value we put it into the request scope. Later on the c:forEach tag can use the data, iterates it row by row to form an HTML table. Our weather data consist of the date, condition and the high and low temperature.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="" prefix="c" %>
    <title>Weather Page</title>
    String[][] data = {
            {"Nov 6", "Sunny", "32", "26"},
            {"Nov 7", "Sunny", "32", "26"},
            {"Nov 8", "Sunny", "32", "26"},
            {"Nov 9", "Partly Cloudy", "32", "26"},
            {"Nov 10", "Isolated T-Storms", "32", "26"}
    request.setAttribute("weathers", data);
<strong>5-Days Weather for Denpasar, Indonesia</strong>

<table border="1">
        <th>TEMP. HIGH</th>
        <th>TEMP. LOW</th>
    <c:forEach var="weather" items="${weathers}">
            <td align="center">${weather[2]}&#x2103;</td>
            <td align="center">${weather[3]}&#x2103;</td>

Our JSP page above creates the following output:

5-Days Weather for Denpasar, Indonesia

Nov 6 Sunny 32℃ 26℃
Nov 7 Sunny 32℃ 26℃
Nov 8 Sunny 32℃ 26℃
Nov 9 Partly Cloudy 32℃ 26℃
Nov 10 Isolated T-Storms 32℃ 26℃