Skip to content

Iterate List in Reverse order in Java Example

Iterate List in reverse order or backward direction Java example shows how to iterate list like LinkedList or ArrayList in reverse or backward direction.

How to iterate a list in reverse order or backward direction?

Let’s first create a List object and populate it with some elements. We are going to use the Student class objects to populate the List.

There are several ways in which you can iterate a list in reverse direction as given below.

1) Using for loop

You can use for loop with the index starting at the list size – 1 and index decremented by one with each iteration till it becomes zero as given below.

The List index starts from 0 and goes up to the size of the list – 1. We are decreasing index by 1 with each iteration till the index becomes 0. The loop prints elements of a List in reverse direction i.e. last element first and so on.

2) Using ListIterator

You can use the ListIterator class to iterate a list in reverse direction using below given method.

This method returns a list iterator which starts at the specified position in the List.

In our case, since we want to traverse a list in the backward direction, we are going to set the list index after the last element and iterate the list using the hasPrevious and previous methods as given below.

3) Using the reverse method of the Collections class

You can first reverse the list using the reverse method of the Collections class and then print the list using for loop or enhanced for loop like given below.

4) Using the descendingIterator method

If you are using the LinkedList instead of an ArrayList, you can get the reverse Iterator using the descendingIterator method of the LinkedList class. The iterator returned by this method returns list elements in reverse order.

5) Using the Apache Commons

If you are using the Apache Commons Collections library, you can use the ReverseListIterator to iterate the List in reverse direction as given below.

6) Using a custom Iterator implementation

You can create your own Iterator implementation which allows iterating a list in reverse direction. We need to implement the Iterable and Iterator interfaces in order to create a custom Iterator implementation as given below.

A custom Iterator must implement three methods hasNext, next and remove. Basically, we have wrapped the ListIterator into a custom Iterator. So we internally call the hasPrevious method of  the ListIterator when the hasNext method of custom iterator is called. Similarly, we call the previous method of the ListIterator when the next method of custom iterator is called. Here is how to use it in your program.

This example is a part of the Java ArrayList tutorial.

Please let me know your views in the comments section below.

About the author

2 comments

  1. Hey,
    thanks for showing set of posibillities.
    Before it get lost in 6) is a error in the class l.37 should be get previous.
    Interesting are the O-Calculus for the variants for preparation for a full iteration. I will try to give my thoughts on them.
    Let N be the size of the list
    1) O(N^2) because in the middle of the list get() should cost N/2
    2) O(N)
    3) O(2N)=O(N)
    4) 5) O(N)
    6) O(N)

    1. Hello Rotfuchs,

      Yes, you are right, it should be previous() instead of next() call. I fixed the code.

      Thanks again for spotting the error.

Leave a Reply

Your email address will not be published.