Iterators and foreach loop
In the Java programming language, both iterators and foreach loops are used to iterate or loop over collection data types such as lists, sets, maps, and other data types containing a series of values. Iterators and foreach loops in Java are built-in features of the collection framework and provide Java users with multiple methods and concepts for traversing collection elements in Java programs.

Iterator in Java.
A loop iterator is an object class method that helps Java users traverse a collection data type such as a list, set, or queue data structure, and remove elements from the collection type in an optional order during a program loop from start to end.
The Java Iterator interface provides Java users with three primary methods.
- hasNext() – Returns true if there are more object elements in the iteration process.
- next() – Returns the next element in the iteration process in a Java program
- remove() – Removes the last element returned by an iterator in a Java program, as an optional operation.
Special Features of Java Iterators.
The iteration process in the Java language is specifically designed or implemented to iterate over collection data type element values.
This allows Java programs to delete data elements in a secure order while iterating over values in a loop, which is not possible with a standard, regular for loop. Iterators in Java can be used with any collection data type element class, which helps implement the Iterable interface in existing programs. For example, ArrayList, HashSet, LinkedList, etc. are data types.
Example of Java Iterators.
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorExplanation {
public static void main(String[] args) {
// here we Creating a collection ArrayList set data type element
ArrayList<String> list = new ArrayList<>();
list.add(“Java”);
list.add(“Python”);
list.add(“Matlab”);
list.add(“Ruby”);
// here we Getting an iterator for the list element
Iterator<String> iterator = list.iterator();
//here we Iterating through the list using the iterator
while (iterator.hasNext()) {
String programming = iterator.next();
System.out.println(programming);
// Alternative – here we Removing an arraylist element during the iteration process
if (programming.equals(“Python”)) {
iterator.remove(); // This will remove “Python” from the arraylist element list
}
}
System.out.println(“Here are the List after iteration process – ” + list);
}
}
In the Java Iterators example.
- In the Java loop iterator program, the hasNext() method is used to check if there are more elements in the data set.
- In the Java loop iterator program, the next() function method extracts the next element.
- In the Java loop iterator program, the remove() function method is used to remove the element “Python” from the list during loop iteration.
For-each Loop/Enhanced For Loop in Java.
In modern Java programming, the for-each loop iteration, also known as the enhanced for loop, provides a simpler and shorter method for iterating over collection data types like lists, sets, maps, and more. The for-each loop iteration was introduced in Java 5 and eliminates the need for an explicit loop iterator process. The for-each loop iteration is typically used when a Java user needs to access or traverse every element or value in a collection data type, and the loop logic or condition does not require removing or modifying the collection data type elements while iterating through the program.
Syntax of a Java for-each loop.
for (Type element : collection) {
// here we can access the list element
}
In a Java for-each loop.
- In the Java for-each loop syntax, Type defines the data type of the elements in the collection.
- In this, element is a program data type variable that holds and processes each element in the collection as the loop iterates.
- In the Java for-each loop iteration process, collection is the collection or array set list data type that is being iterated over in the current program.
Special features of the Java for-each loop.
- The Java for-each loop iteration process easily implements the iteration task on an array or collection of data.
- The Java for-each loop iteration process is a read-only loop. You cannot modify the collection data type or remove any existing data elements from the collection during the iteration process.
- The Java for-each loop iteration process can be used with arrays and any collection list data type that implements the Iterable interface concept.
Example of a Java for-each loop.
import java.util.ArrayList;
public class ForEachLoopIllustration {
public static void main(String[] args) {
/ here we are creating a collection of ArrayList element data type
ArrayList<String> list = new ArrayList<>();
list.add(“Maruti Suzuki”);
list.add(“Hyundai”);
list.add(“Tata Motors”);
list.add(“Mahindra”);
list.add(“Toyota”);
// Here we are using a for-each loop to iterate through the ArrayList element data type.
System.out.println(“Here we use a for-each loop -“);
for (String carbrand : list) {
System.out.println(carbrand);
}
}
}
Here is the for-each loop example.
In this program, the for-each loop iterates over the values or elements in the ArrayList, printing each element to the console screen. Remember, the for-each loop does not provide any special method for removing or modifying elements in the ArrayList collection during the iteration process.
Main Differences Between Iterator and For-each Loop
| Each Feature | Iterator explanation | For-each Loop explanation |
| Each Syntax | Java Iterator Uses hasNext(), next(), and remove() popular function methods to iterate list object | Java For-each LoopUses a simple syntax – for (element : collection) in program |
| Modifying the Collection or not | Java Iterator capable to remove elements during iteration using remove() function method | Java For-each LoopCannot modify the collection (e.g, no remove()) function method |
| Where to Use Case | Java Iterator is Suitable when java user need to remove elements during iteration process or need more control over iteration loop | Java For-each LoopSimplifies iteration process, when you just need to read elements list data type |
| Usages Performance | Java Iterator allow Slightly more overhead due to method calls (hasNext(), next()) function | Java For-each loop More detailed and easier to read, but not suitable for removal of elements during iteration process |
| Null Handling features | Java Iterator Works with collections list data type that allow null values | Java For-each loop Works with collections data type that also allow null values |
| Flexibility or not | Java Iterator is More flexible, as you can perform more complex list collection data type operations | Java For-each loop More limited function, just used for simple program iteration task |
Summary of the for-each loop usage in Java programming.
- Iterator – Use for-each loop in Java user program when Java programmer needs complete control over the loop iteration process. For example, removing elements during loop element iteration process or if Java user needs to modify list collection data type during loop iteration process etc.
- For-each loop – Use for-each loop in Java user program when Java user needs to iterate only list element values and does not need to modify list data collection during loop iteration process. For-each loop in Java program is more easily readable and shorter option for easy loop iteration task.
