The objects in Java are iterated through the iterable interface. The loops in Java play a vital role in iterating over data structures like Array, HashMap, and Stack. Java loops allow us to run over the same code repeatedly every time with a different value. There are various looping methods in Java and are used when needed. One of these loops is the for-each loop that traverses the element in the collection.
This article will implement the for-each loop in Java.
Implementation of for-each Loop in Java
The for-each loop in Java was introduced in Java 5 and is also referred to as an “enhanced for loop”. The for-each loop iterates over the elements without explicitly defining the index value as in the for loop. This leads to simplicity in the code which increases the readability of the code due to its easy and short syntax.
Below is the syntax of the for-each loop in Java
for(Typeofdata element : iterable) {
// Code to be inserted
}
In the above syntax, the type of data refers to the String or Integer values and the iterable refers to the array or list for iteration.
Now we will advance and move towards the implementation of the for-each loop in Java.
Example 1: Basic Implementation of for-each Loop in Java
The code example below depicts that the for-each loop is applied to iterate over the elements in the array.
//Import the required package
import java.io.*;
//Declare a Class to implement the for-each loop
class ForEachLoop{
//Main class of Java
public static void main(String[] args)
{
System.out.println("Basic Implementation of for-each Loop");
//Declare an array and insert the elements
int array[] = {10, 20, 30, 40, 50};
//for-each loop of Java
for (int element : array)
//Print the output
System.out.println(element + " ");
}
}
In the above code block:
- The package required for the loop is declared.
- In the next step, a class is declared as “ForEachLoop”.
- The integer array is declared as an “array” and values are inserted into the arrays.
- The for loop iterates over the array and prints the element.
Output
The below output shows that the integers 10, 20, 30, 40, and 50 are printed using the for-each loop of Java.
Example 2: Sum of Numbers in the Array Using for-each Loop
The code example below describes how for-each is used to calculate the sum of the integers specified in the array.
class ForEachLoop1{
//Declare the main() method of Java
public static void main(String args[]){
//Declare an array and add integer values.
int array[]={1,2,15,35,16};
//Initialize the array as 0
int sum=0;
for(int i:array){
//add the integer values in the array
sum=sum+i;
}
//Print the output
System.out.println("The total is : " +sum);
}
}
In the above code:
- A class is declared as “ForEachLoop” to calculate the sum of numbers in an array.
- In the main() method of Java, an integer array is declared.
- The sum is initialized as 0 and using the for each loop, the numbers in the array are added.
- The result is printed as the addition of integer values in the array.
Output
The below output depicts that the sum is printed as 69.
Example 3: Implementation of for-each Loop on HashMap
The code example describes how for-each is used in HashMap to iterate over the key and print the values respectively.
import java.util.*;
public class ForEachLoop{// main() method
public static void main(String args[]) {
// Creating a HashMap
HashMap<String, String> pl = new HashMap<>();
pl.put("First Language", "Java");
pl.put("second Language", "Python");
pl.put("Third Language", "Ruby");
//Iteration using the keys
System.out.print("Keys: ");
//The for loop to get the key
for (String key : pl.keySet()) {
//Print the output for key
System.out.print(key + ", ");
}
System.out.println();
// Iterating using values
System.out.print("Values: ");
//The for loop to get the values
for (String value : pl.values()) {
//Print the output for Value
System.out.print(value + ", ");
}
}
}
In the above Java code:
- The required package is imported and the class is declared.
- The put() method inserts the key-value pair to the HashMap.
- The first for-each loop provides the keys of the HashMap.
- The next for loop provides the values of the HashMap.
- The key-value pair of the HashMap is printed accordingly.
Output
The below output prints the results in a manner that the Third Language corresponds to Ruby, the First Language to Java, and, the second Language corresponds to Python respectively.

Conclusion
The for-each loop in Java is implemented in such a manner that it iterates over the elements in an iterable without using the index number. In this article, we have implemented the for-each loop on data structures like Array and HashMap to iterate the elements and the key-value pair respectively.