1. Introduction
In this article, we will see how to traverse the List in a few different ways. This includes traversing using Iterator and without Iterator. Traversing using the Iterator means external iteration, and traversing without the Iterator means internal iteration.
2. Traditional for loop
ArrayList is backed by an array and hence provides indexed access to the elements. We use that concept to traverse the elements in the ArrayList by using get(int index) method. This is an external iteration, as we use a looping construct to traverse the List.
List<Transaction> transactions = TransactionDataSet.getTxns(); for (int i = 0; i < transactions.size(); i++) { System.out.println(transactions.get(i)); }
3. For-each loop
The for-each loop construct was added to Java 5. This is an external iteration, as we use a looping construct to traverse the List.
List<Transaction> transactions = TransactionDataSet.getTxns(); for (Transaction transaction : transactions) { System.out.println(transaction); }
4. Using Iterator<>
Over here we are traversing the List using the iterator. The implementation of the Iterator depends on underlying Collection. We use Iterator’s methods hasNext() and next() to check if elements exist and traverse to that element, respectively. This is an external iteration, as we use a looping construct to traverse the List.
List<Transaction> transactions = TransactionDataSet.getTxns(); Iterator<Transaction> txnIterator = transactions.iterator(); while(txnIterator.hasNext()) { System.out.println(txnIterator.next()); }
5. Using ListIterator<>
ListIterator is an interesting Iterator. It allows us to traverse the List in a forward and backward way. It supports several APIs, such as hasNext(), next(), nextIndex(), hasPrevious(), previous(), previousIndex(), remove(), set(E), add(E). The below example is traverses the List in forward manner, i.e. using hasNext() and next() method. This is an external iteration, as we use a looping construct to traverse the List.
List<Transaction> transactions = TransactionDataSet.getTxns(); ListIterator<Transaction> txnListIterator = transactions.listIterator(); while(txnListIterator.hasNext()) { System.out.println(txnListIterator.next()); }
6. Using ListIterator<> to traverse in reverse
In this example, the ListIterator traverses the List in reverse way hence, we are using methods hasPrevious() and previous(). This is an external iteration, as we use a looping construct to traverse the List.
List<Transaction> txns = TransactionDataSet.getTxns(); ListIterator<Transaction> iter = txns.listIterator(transactions.size()); while(iter.hasPrevious()) { System.out.println(iter.previous()); }
7. Using the forEach method
forEach method was added to Iterable interface in Java 8. forEach method is an example of Internal Iteration where a framework will traverse the Collection and we just need to provide the action that will be performed for every element.
List<Transaction> transactions = TransactionDataSet.getTxns(); transactions.forEach(txn -> System.out.println(txn));
8. Conclusion
In this article, we saw several ways to traverse the List. As of the Java 8 method forEach in the Iterable interface, the traversal in any Collection is much more painless as we do not need to use Iterator or ListIterator. We can just provide an action to perform for every element and let the framework do the iteration.