The for-each loop in Java, also known as the enhanced for loop, is a simplified version of the regular for
loop. It is primarily used to iterate over elements in arrays or collections (such as List
, Set
, Map
, etc.), without the need to deal with indexes or manually incrementing a counter. This makes the for-each loop more readable and reduces the chance of errors in code.
In this guide, we will explore how the Java for-each loop works, its syntax, and provide examples to help you understand its usage.
The for-each loop is an enhanced version of the traditional for
loop, specifically designed for iterating through arrays or collections (like lists and sets) without needing to manually manage the loop index. The loop iterates over each element in the array or collection and performs an action for each item.
The main advantage of the for-each loop is that it simplifies the code and makes it more readable by abstracting away the details of index handling.
The syntax of the for-each loop is simple and involves the following components:
for (type element : array_or_collection) {
// code to be executed
}
int
, String
, Double
).List
, Set
, etc.) over which the loop iterates.for (int number : numbers) {
System.out.println(number);
}
In this example, numbers
could be an array or a collection, and the loop will print each number in it one by one.
Let’s see a few practical examples of using the Java for-each loop to iterate over different types of data structures.
public class ForEachArrayExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
// Using the for-each loop to iterate through the array
for (int num : numbers) {
System.out.println(num);
}
}
}
Explanation:
numbers
array.num
during each iteration, and then printed.1
2
3
4
5
import java.util.List;
import java.util.ArrayList;
public class ForEachListExample {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
// Using the for-each loop to iterate through the list
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
Explanation:
List
of fruits is created, and the for-each
loop iterates through each element.fruit
in the list is printed in the loop.Apple
Banana
Cherry
The for-each loop can also be used with other collections like Set
and Map
. It makes it easy to iterate through elements in these data structures without needing an explicit iterator or index.
import java.util.Set;
import java.util.HashSet;
public class ForEachSetExample {
public static void main(String[] args) {
Set<String> colors = new HashSet<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
// Using the for-each loop to iterate through the Set
for (String color : colors) {
System.out.println(color);
}
}
}
Explanation:
Set
colors
.Red
Green
Blue
Lists
, Sets
, and other collections that implement the Iterable
interface.element
is read-only. You cannot modify the collection (e.g., add or remove elements) while iterating over it.ArrayList
or LinkedList
, when you need to use the index.List
, Set
, and other iterable collections where you only need to process elements sequentially.