How to Iterate Over List in Java: Simple Syntax and Examples
To iterate over a list in Java, you can use a
for-each loop which goes through each element one by one. Alternatively, you can use an Iterator or Java 8's stream() method to process list elements.Syntax
Here are common ways to iterate over a list in Java:
- For-each loop: Simplest way to access each element.
- Iterator: Gives more control, can remove elements safely.
- Stream API: Modern way to process elements with functional style.
java
import java.util.List; import java.util.Iterator; List<String> list = List.of("apple", "banana", "cherry"); // For-each loop for (String item : list) { System.out.println(item); } // Iterator Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } // Stream API list.stream().forEach(System.out::println);
Output
apple
banana
cherry
apple
banana
cherry
apple
banana
cherry
Example
This example shows how to print all elements of a list using a for-each loop.
java
import java.util.List; public class IterateListExample { public static void main(String[] args) { List<String> fruits = List.of("apple", "banana", "cherry"); for (String fruit : fruits) { System.out.println(fruit); } } }
Output
apple
banana
cherry
Common Pitfalls
Common mistakes when iterating over lists include:
- Modifying the list inside a for-each loop, which causes
ConcurrentModificationException. - Using a regular for loop with incorrect index bounds.
- Not checking if the list is empty before iterating, which is usually safe but sometimes leads to logic errors.
Use an Iterator if you need to remove elements during iteration.
java
import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class PitfallExample { public static void main(String[] args) { List<String> list = new ArrayList<>(List.of("a", "b", "c")); // Wrong: modifying list inside for-each loop causes error // for (String s : list) { // if (s.equals("b")) { // list.remove(s); // Throws ConcurrentModificationException // } // } // Right: use Iterator to remove safely Iterator<String> it = list.iterator(); while (it.hasNext()) { if (it.next().equals("b")) { it.remove(); } } System.out.println(list); // Output: [a, c] } }
Output
[a, c]
Quick Reference
Summary of ways to iterate over a list in Java:
| Method | Description | When to Use |
|---|---|---|
| For-each loop | Simple and readable way to access each element | Most common cases |
| Iterator | Allows safe removal of elements during iteration | When modifying list while iterating |
| Stream API | Functional style processing with lambda expressions | For complex data processing or chaining operations |
| Classic for loop | Using index to access elements | When index is needed or list is mutable |
Key Takeaways
Use for-each loop for simple and clear iteration over lists.
Use Iterator to safely remove elements during iteration.
Stream API offers a modern, functional way to process list elements.
Avoid modifying a list inside a for-each loop directly to prevent errors.
Classic for loops are useful when you need the index of elements.