Java forEach Loop

The forEach in Java

  • The foreach loop is generally used for iteration through array elements in different programming languages.
  • Java provides arrays as well as other collections and there should be some mechanism for going through array elements easily; like the way foreach provides.
  • In Java 8, the forEach statement is part of the new Stream API introduced to enhance the functionality of collections.

The forEach statement in Java 8

In Java 8, the new forEach statement is provided that can be used to loop the maps or list, etc.

You may loop a list with forEach and lambda expressions.

An example with lambda expression:

import java.util.*;

public class loop_demos {

    public static void main(String []args) {

                List<String> lstdemo = new ArrayList<>();

                lstdemo.add("Iterate");
                lstdemo.add("Through");
                lstdemo.add("A");
                lstdemo.add("List");
                lstdemo.add("Collection");

                lstdemo.forEach(lstitem->System.out.println(lstitem));

    }

}

The output of the program should be:

Iterate, Through, A, List, Collection

Using enhanced for loop

The purpose of foreach can also be accomplished by using the enhanced form of the for loop that enables us to specify an array or other collections and work with its elements.

  • The enhanced for loop of Java works just like the foreach loop in that a collection is specified in the for loop.
  • The current element can be assigned to a variable inside the for loop.
  • You may perform a certain action with that element and execution moves to the next item and the process goes on until all elements are entertained.
  • See the following section for a demonstration of going through each element of the array and other collections.

A demo of iterating through each item of array

In this example, a Java array of six elements is created with numbers. After that, a for loop is used to iterate through each item of the array:

The code to iterate through each array element:

public class loop_demos {

    public static void main(String []args) {
        int[] arrNumberss = {1,5,10,15,20,25};

        for (int arrElement : arrNumberss) {

            System.out.println("Item: " + arrElement);

        } 
    }
}

Output:

Java foreach array

A demo of iterating through list items by “forEach”

In this example, each element of the list is accessed and displayed by using an enhanced for loop.

The code of building a list and iterating through its items by using a for loop:

import java.util.*;

public class loop_demos {

    public static void main(String []args) {

                List<String> lstdemo = new ArrayList<>();

                lstdemo.add("Iterate");
                lstdemo.add("Through");
                lstdemo.add("A");
                lstdemo.add("List");
                lstdemo.add("Collection");

                for(String lstitem : lstdemo){

                                System.out.println("List item: " + lstitem);
                } 
    }
}

Output:

foreach list

The final word

Before you start using the forEach Java 8 function rather than the enhanced for loop for iterating through arrays, lists, maps, or other collections, think carefully about the performance, advantages, and disadvantages of both for different scenarios.

You may download the Java 8 here.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!