π Java ArrayList Loop β Different Ways to Iterate with Examples
π§² Introduction
Looping through an ArrayList
is one of the most common operations in Java. Whether you’re displaying items, applying calculations, or filtering data, understanding different looping methods is essential.
In this guide, youβll learn how to:
- β Loop through an ArrayList using multiple approaches
- β Understand the advantages of each method
- β Apply clean and readable code practices
π Sample ArrayList
Before we dive into looping techniques, letβs create a sample ArrayList
:
import java.util.ArrayList;
public class LoopArrayList {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
fruits.add("Grapes");
}
}
β
Weβll use this fruits
list in all the looping examples below.
π 1. For Loop (Index Based)
for (int i = 0; i < fruits.size(); i++) {
System.out.println(fruits.get(i));
}
β Explanation:
fruits.size()
gives the total number of elements.fruits.get(i)
retrieves each element by index.
π‘ Use when you need the index during iteration.
π 2. Enhanced For Loop (For-Each)
for (String fruit : fruits) {
System.out.println(fruit);
}
β Explanation:
- Clean and readable syntax.
- Automatically fetches each element.
π‘ Best for readability and when index is not needed.
π 3. While Loop with Iterator
import java.util.Iterator;
Iterator<String> iterator = fruits.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
β Explanation:
hasNext()
checks if more elements exist.next()
retrieves the next element.
β οΈ Use this when modifying the list during iteration (like removing elements).
π 4. Java 8 forEach + Lambda
fruits.forEach(fruit -> System.out.println(fruit));
β Explanation:
- Uses
forEach()
from Java 8 with a lambda. - Very concise and modern.
π‘ Great for clean, functional-style programming.
π§ͺ 5. Using ListIterator (Bidirectional Traversal)
import java.util.ListIterator;
ListIterator<String> listIterator = fruits.listIterator();
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
β Explanation:
ListIterator
supports forward and backward movement.- Can be used to edit elements while iterating.
π Comparison Table
Loop Type | Supports Index? | Removes Elements Safely? | Java Version |
---|---|---|---|
For Loop | β Yes | β οΈ No | All |
Enhanced For Loop | β No | β οΈ No | Java 5+ |
While with Iterator | β No | β Yes | Java 2+ |
forEach + Lambda | β No | β οΈ No | Java 8+ |
ListIterator | β Yes (both ways) | β Yes | Java 2+ |
π Summary
In this article, you explored:
- β
Five ways to loop over a Java
ArrayList
- β When to use each method effectively
- β Tips for safe iteration and clean code
Mastering these looping techniques will help you write efficient and clean Java code when working with collections.
βFAQ β Java ArrayList Looping
βWhich is the best loop for performance?
The traditional for
loop is generally fastest for primitive types, but for objects like ArrayList, differences are negligible unless working with very large datasets.
βCan I remove items during a loop?
Use Iterator
or ListIterator
to safely remove items during iteration.
βIs Java 8 forEach
better than loops?
Yes, for readability and functional programming style, but not ideal if you need to break or continue inside the loop.
Share Now :