Iterate List in reverse order or backward direction Java example shows how to iterate list (LinkedList, ArrayList) in reverse order or backward direction in Java using various approaches.
How to iterate list in reverse order or backward direction?
Let’s first create a List and populate it with some objects. We are going to use example 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 list in reverse direction as given below.
1) Iterate list in reverse order using for loop
You can use for loop with index starting at (size of the list – 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) ); } |
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 index becomes 0. The loop prints elements of a List in reverse direction i.e. last element first and so on.
2) Iterate list in reverse order using ListIterator
You can use ListIterator to iterate a list in reverse direction using below given method.
1 | ListIterator<E> listIterator( int listIndex ) |
This method returns a listIterator which starts at the specified position in the List.
In our case, since we want to traverse a list in backward direction, we are going to set the listIndex after the last element and iterate the list using 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) Iterate list in reverse order using reverse method of Collections class
You can reverse the list first using reverse
method of 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) Iterate LinkedList in reverse order using descendingIterator method
If you are using LinkedList instead of ArrayList, you can get the reverse Iterator using descendingIterator
method of 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) Iterate list in reverse order using Apache Commons
If you are using Apache Commons Collections library, you can use ReverseListIterator to iterate 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) Iterate list in reverse order using Custom Iterator
You can create your own Iterator implementation which allows iterating a list in reverse direction. We need to implement Iterable
and Iterator
interfaces in order to create a custom Iterator 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.next(); } public void remove() { listIterator.remove(); } }; return reverseIterator; } } |
A custom Iterator must implement three methods hasNext
, next
and remove
. Basically, we have wrapped ListIterator into a custom Iterator. So we internally call hasPrevious
method of listIterator when hasNext
method of custom iterator is called. Similarly, we call previous
method of ListIterator when 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); |
See Also: How to iterate over List example
Please let us know your views in the comments section below.
Add Comment