Iterating over a list is a common task in most programs. This shot will focus on the different ways to iterate over a List
object in Java.
There are a number of different ways to iterate over a list:
for
loopfor
loopforEach
iterablesfor
loopYou can iterate over a list with a simple for
loop. A for
loop has three parts to it: the initializing statement, the loop condition check statement, and the increment statement, as shown below:
The code below shows how you can use a basic for
loop to iterate over a list in Java.
import java.util.List;import java.util.ArrayList;class HelloWorld {public static void main( String args[] ) {List<Integer> numbers = new ArrayList<>();numbers.add(1);numbers.add(2);numbers.add(3);numbers.add(4);numbers.add(5);numbers.add(6);numbers.add(7);numbers.add(8);for(int i=0; i<numbers.size(); i++) {System.out.print(numbers.get(i)+ " ");}}}
for
loopThe enhanced for
loop is a more compact form of the basic for
loop that can be used to iterate over a list, as shown below:
import java.util.List;import java.util.ArrayList;class HelloWorld {public static void main( String args[] ) {List<Integer> numbers = new ArrayList<>();numbers.add(1);numbers.add(2);numbers.add(3);numbers.add(4);numbers.add(5);numbers.add(6);numbers.add(7);numbers.add(8);for(int i: numbers) {System.out.print(i+ " ");}}}
Modifying the list while iterating on it with the above constructs leads to the
ConcurrentModificationException
.
Iterators can also be used to iterate over a list. An iterator is especially useful when you need to modify the list that is being iterated on.
import java.util.List;import java.util.ArrayList;import java.util.Iterator;class HelloWorld {public static void main( String args[] ) {List<Integer> numbers = new ArrayList<>();numbers.add(1);numbers.add(2);numbers.add(3);numbers.add(4);numbers.add(5);numbers.add(6);numbers.add(7);numbers.add(8);Iterator<Integer> itr = numbers.iterator();while(itr.hasNext()) {int i = itr.next();System.out.print(i+ " ");if(i==3) {itr.remove();}}}}
The two important methods for an iterator are hasNext()
and next()
. The above snippet shows how these two can be used to iterate over the list.
Java also provides the listIterator
class, which can be used to traverse a list in both directions, as shown below:
import java.util.List;import java.util.ArrayList;import java.util.ListIterator;class HelloWorld {public static void main( String args[] ) {List<Integer> numbers = new ArrayList<>();numbers.add(1);numbers.add(2);numbers.add(3);numbers.add(4);numbers.add(5);numbers.add(6);numbers.add(7);numbers.add(8);ListIterator<Integer> litr = numbers.listIterator();while(litr.hasNext()) {int i = litr.next();System.out.print(i+ " ");}System.out.print("\n");while (litr.hasPrevious()) {int i= litr.previous();System.out.print(i+ " ");}}}
forEach
iterableThe introduction of lambdas
in Java led to the addition of the forEach
function, which can be used to iterate over any collection easily.
The code below shows how the forEach
method can be used to iterate over a list:
import java.util.List;import java.util.ArrayList;class HelloWorld {public static void main( String args[] ) {List<Integer> numbers = new ArrayList<>();numbers.add(1);numbers.add(2);numbers.add(3);numbers.add(4);numbers.add(5);numbers.add(6);numbers.add(7);numbers.add(8);numbers.forEach(System.out::println);}}