For Each loop in Java

Java 5 brought in the support in the support for foreach loop that lets the developers to iterate over collections pretty easily.

The syntax for the foreach loop in java looks like the one shown below.

for (<datatype> item: collection)
{
// Process the item
}

For Each loop in Java

Below is an example that demonstrates foreach loop in java.

package com.abundantcode;

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

public class Main {

    public static void main(String[] args)
    {
        List<String> input = new ArrayList<String>();
        input.add("Abundantcode.com");
        input.add("Programming Website");
        input.add("Java Tutorials");
        for(String item:input)
        {
            System.out.println(item);
        }
    }
}

image

%d