Iterate List in reverse order in Java
This post will discuss various ways to iterate a list in reverse order in Java without actually reversing the list.
1. Using ListIterator
The List interface provides a special iterator, called ListIterator, that allows bidirectional access. We can call the List.listIterator(index) method to get a ListIterator over the list elements starting from the specified position in the list. Since we need to start from the last element, the starting index would be equal to the list’s size.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.List; import java.util.ListIterator; class Main { // Java program to iterate list in reverse order public static void main(String[] args) { List<String> list = Arrays.asList("C", "C++", "Java"); // use `ListIterator` to iterate list in reverse order ListIterator<String> itr = list.listIterator(list.size()); // `hasPrevious()` returns true if the list has a previous element while (itr.hasPrevious()) { System.out.println(itr.previous()); } } } |
2. Using Apache Common Collections
Apache Common provides ReverseListIterator that we can use to iterate list in reverse order.
The first call to next() will return the last element from the list, the next call to next() will return the previous element, and so on. hasNext() method checks if there is another element.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import org.apache.commons.collections4.iterators.ReverseListIterator; import java.util.Arrays; import java.util.List; class Main { // Java program to iterate list in reverse order public static void main(String[] args) { List<String> list = Arrays.asList("C", "C++", "Java"); ReverseListIterator itr = new ReverseListIterator(list); while (itr.hasNext()) { System.out.println(itr.next()); } } } |
3. Using for-loop
We know that List is an ordered collection, so we can access elements by their index (position) in the list using a for-loop. Please note we can’t use enhanced for-loop here to iterate from the end at the beginning of a List as it uses an Iterator behind the scenes, and Iterator is not bidirectional.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.List; import java.util.ArrayList; import java.util.Arrays; class Main { // Java program to iterate list in reverse order public static void main(String[] args) { List<String> list = Arrays.asList("C", "C++", "Java"); // print list backward using for-loop for (int i = list.size() - 1; i >= 0 ; i--) { System.out.println(list.get(i)); } } } |
4. Java 8 – Using streams
In Java 8, we can:
- Get stream using
List.stream(). - Accumulate elements of this stream into a
LinkedListusingStream.collect(). - Get an iterator over the elements in the
LinkedListin reverse sequential order usingLinkedList.descendingIterator()method. - Perform the print operation on elements of the list using
Iterator.forEachRemaining()until all elements have been processed. We can either provide method referenceSystem.out::printlnor pass lambda expressionS -> System.out.println(S)toIterator.forEachRemaining().
The following program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; import java.util.LinkedList; import java.util.List; class Main { // Java program to iterate list in reverse order public static void main(String[] args) { List<String> list = Arrays.asList("C", "C++", "Java"); new LinkedList<>(list) // LinkedList .descendingIterator() // Iterator .forEachRemaining(System.out::println); } } |
We can also write a collector that collects elements in the reversed order, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; class Main { // Java program to iterate list in reverse order public static void main(String[] args) { List<String> list = Arrays.asList("C", "C++", "Java"); list.stream() .collect(Collectors.collectingAndThen(Collectors.toList(), lst -> { Collections.reverse(lst); return lst.stream(); } )) .forEach(System.out::println); } } |
That’s all about iterating over a List in reverse order in Java.
Related Post:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)