Java: Methods on how to iterate over a List
Following we present a few methods on how to iterate over a List in Java.
Currently we present:
- Using a standard for loop
- Using an iterator to loop
- Using a For-Each loop
- Using Streams
[download id=”2234″]
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class ListLooper {
public static void main(final String[] argv) {
final String elementsArray[] = new String[] { "First Element", "Second Element", "Third Element" };
// First of all we convert an array of Strings to a list of Strings, we do this to avoid adding each element to the list we will use by using the add() method.
final List<String> elementsList = Arrays.asList(elementsArray);
// Method 1: Using a standard for loop
System.out.println("Method 1: Using a standard for loop");
// We will loop N times, where N is the size of the list.
// Since the first element of the list is on position 0, we start from that and finish at position N-1.
for (int i = 0; i < elementsList.size(); i++) {
// Using get() we retrieve the element at position.
System.out.println(elementsList.get(i));
}
// Method 2: Using an iterator to loop
System.out.println("Method 2: Using an iterator to loop");
// The Java iterator is an interface that belongs to the collection framework and allows us to traverse a collection and access the data element of the collection without bothering the user about the implementation details of that collection.
final Iterator<String> iterator = elementsList.iterator();
while (iterator.hasNext()) {
// next() returns the next element in the collection until the hasNext() method returns false.
System.out.println(iterator.next());
}
// Method 3: Using a For-Each loop
System.out.println("Method 3: Using a For-Each loop");
// This code works for any object that implements the Iterable interface.
for (final String element : elementsList) {
System.out.println(element);
}
// Method 4: Using Streams
System.out.println("Method 4: Using Streams");
// This code will not work for Java versions earlier than Java 8.
elementsList.forEach((element) -> {
System.out.println(element);
});
}
}
[download id=”2234″]
Compilation and execution are presented in the next section.
javac ListLooper.java; java ListLooper; Method 1: Using a standard for loop First Element Second Element Third Element Method 2: Using an iterator to loop First Element Second Element Third Element Method 3: Using a For-Each loop First Element Second Element Third Element Method 4: Using Streams First Element Second Element Third Element
Following is the Java version used in this article
java -version openjdk version "1.8.0_111" OpenJDK Runtime Environment (build 1.8.0_111-b16) OpenJDK 64-Bit Server VM (build 25.111-b16, mixed mode)


