In Java, there are 2 main loop constructs: for and while. Let us review each of these in turn.
for Loop
Since Java 5, there are 2 forms of for loop: classic and enhanced. The classic for loop is in the form:
for (initialization; termination; increment/decrement) {
statement(s)
}
The enhanced for loop is in the form:
for (type : collection) {
statement(s)
}
The major difference between the 2 for loops is that the classic for loop allows us to keep track of the index or position of the collection.
while Loop
There are again 2 forms of while loop: while and do-while. The while loop is in the form:
while (expression) {
statement(s)
}
The do-while loop is in the form:
do {
statement(s)
} while (expression);
The major difference between the 2 while loops is that the do-while will execute at least once.
Concept of the Iterator
An iterator is an object that enables us to traverse a collection. There is an iterator (java.util.Iterator) in all the top level interfaces of the Java Collections Framework that inherits java.util.Collection interface. These interfaces are java.util.List, java.util.Queue, java.util.Deque, and java.util.Set. Furthermore, there is the java.util.Map interface that does not inherit java.util.Collection.
Lists also have a special iterator called a list iterator (java.util.ListIterator). What’s the difference? The java.util.Iterator is forward looking only while the java.util.ListIterator is bidirectional (forward and backward). Furthermore, the java.util.ListIterator inherits java.util.Iterator. The result of using either iterator to loop through a list will be the same as we will see later.
Java Collections Looping Approaches
Can you guess how many ways we can loop through a collection given there are 2 forms of loop constructs and all interfaces have an iterator.
The enhanced for loop is the first way and most direct.
for (String s : strList) {
System.out.println(s);
}
Next is the classic for loop, which uses an iterator.
for (Iterator<String> it = strList.iterator(); it.hasNext(); ) {
System.out.println(it.next());
}
Notice that there is no increment/decrement in this classic for loop.We mentioned earlier that Lists have a special iterator called ListIterator. We can easily replace for loop’s initialization to either of the following and it would work as well.
ListIterator<String> it = strList.listIterator();
Iterator<String> it = strList.listIterator();
Next is the while loop, which is basically the classic for loop represented in a different format.
Iterator<String> itA = strList.iterator();
while (itA.hasNext()) {
System.out.println(itA.next());
}
Similarly the iterator declaration (variable itA) can be replaced to either of the following and it would work as well.
ListIterator<String> itA = strList.listIterator();
Iterator<String> itA = strList.listIterator();
Finally the last approach is the do-while loop. We can easily refactor the while loop to become a do-while loop as shown below.
Iterator<String> itB = strList.iterator();
do {
System.out.println(itB.next());
} while (itB.hasNext());
Again the iterator declaration (variable itB) can be replaced with the list iterator form.
Here we are using this deque as a queue. If we were to use it as a stack, then we would have used the push() method to populate the deque.The enhanced for loop:
for (String s : strDeque) {
System.out.println(s);
}
The classic for loop:
for (Iterator<String> it = strDeque.iterator(); it.hasNext(); ) {
System.out.println(it.next());
}
The while loop:
Iterator<String> itA = strDeque.iterator();
while (itA.hasNext()) {
System.out.println(itA.next());
}
The do-while loop:
Iterator<String> itB = strDeque.iterator();
do {
System.out.println(itB.next());
} while (itB.hasNext());
for (Iterator<String> it = strMap.keySet().iterator(); it.hasNext(); ) {
String key = it.next();
System.out.println(key + "=" + strMap.get(key));
}
The while loop:
Iterator<String> itA = strMap.keySet().iterator();
while (itA.hasNext()) {
String key = itA.next();
System.out.println(key + "=" + strMap.get(key));
}
The do-while loop:
Iterator<String> itB = strMap.keySet().iterator();
do {
String key = itB.next();
System.out.println(key + "=" + strMap.get(key));
} while (itB.hasNext());
To summarize, there are 4 major approaches to loop through a collection in general. Lists have 10 approaches because of a) the extra specialized iterator and b) able to retrieve elements directly given its position in the loop.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments