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.
1 2 3 4 5 6 7 8 9 |
//create a List List<Student> listStudents = new ArrayList<Student>(); //add Student objects to the List listStudents.add(new Student("1", "Student1", "10th", 93)); listStudents.add(new Student("2", "Student2", "10th", 54)); listStudents.add(new Student("3", "Student3", "10th", 78)); listStudents.add(new Student("4", "Student4", "10th", 62)); listStudents.add(new Student("5", "Student5", "10th", 46)); |
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.
1 2 3 |
for( int i = listStudents.size() - 1 ; i >=0 ; i-- ){ System.out.println( listStudents.get(i) ); } |
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.
1 |
ListIterator<E> listIterator( int listIndex ) |
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.
1 2 3 4 |
ListIterator<Student> listIterator = listStudents.listIterator( listStudents.size() ); while( listIterator.hasPrevious() ) System.out.println( listIterator.previous() ); |
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.
1 2 3 4 5 |
//iterate list using reverse and enhanced for loop Collections.reverse(listStudents); for(Student student : listStudents) System.out.println(student); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//create a LinkedList List<Student> listStudents = new LinkedList<Student>(); //add Student objects to the LinkedList listStudents.add(new Student("1", "Student1", "10th", 93)); listStudents.add(new Student("2", "Student2", "10th", 54)); listStudents.add(new Student("3", "Student3", "10th", 78)); listStudents.add(new Student("4", "Student4", "10th", 62)); listStudents.add(new Student("5", "Student5", "10th", 46)); Iterator<Student> itrLinkedList = linkedList.descendingIterator(); while(itrLinkedList.hasNext()) System.out.println(itrLinkedList.next()); |
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.
1 2 3 4 |
ReverseListIterator<Student> itr = new ReverseListIterator<Student>(listStudents); while(itr.hasNext()) System.out.println(itr.next()); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
class ReverseIterator<T> implements Iterable<T>{ //define member variable which will hold the original list private final List<T> list; /* * Define a constructor, which accepts the * List parameter */ public ReverseIterator(List<T> list){ this.list = list; } /* * Classes implementing Iterable interface * must define iterator() method which should * return Iterator. */ public Iterator<T> iterator() { //initialize ListIterator and set it after the end of the list final ListIterator<T> listIterator = list.listIterator( list.size() ); /* * create new custom Iterator * which implements Iterator interface */ Iterator<T> reverseIterator = new Iterator<T>(){ public boolean hasNext() { return listIterator.hasPrevious(); } public T next() { return listIterator.previous(); } public void remove() { listIterator.remove(); } }; return reverseIterator; } } |
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.
1 2 3 4 5 6 |
//get custom reverse iterator ReverseIterator<Student> reverseIterator = new ReverseIterator<Student>(listStudents); for(Student student : reverseIterator) System.out.println(student); |
This example is a part of the Java ArrayList tutorial.
Please let me know your views in the comments section below.
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)
Hello Rotfuchs,
Yes, you are right, it should be previous() instead of next() call. I fixed the code.
Thanks again for spotting the error.