Open In App

Iterate List in Reverse Order in Java

Last Updated : 28 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements.

Example

Input: ["geeks", "for", "Geeks"]
Output: ["Geeks", "for", "geeks"]
 
Input: [ 1, 2, 3, 4, 5]
output: [5, 4, 3, 2, 1]

We can iterate the list in reverse order in two ways:

  1. Using List.listIterator() and Using for loop method.
  2.  Using IntStream range(int startInclusive, int endExclusive).

Approach 1: Using List.listIterator() and Using for loop method.

Syntax:

public ListIterator listIterator()

Return Value: This method returns a list iterator over the elements in this list (in proper sequence).

  • This allows bidirectional access.
  • List.listIterator() method is used to get a ListIterator over the elements in a list starting from specified position in the list. If we need to start from the last element, the starting Index would be equal to the size of the list.

Syntax:

ListIterator<Integer> listIterator( Index )

Index = Index from where list element will reverse till index = 0.

Java




// Java program to iterate List in Reverse Order
  
import java.util.*;
  
class GFG {
  
    public static void main(String[] args)
    {
  
        // For ArrayList
        List<String> list = new ArrayList<String>();
  
        // Add elements to list
        list.add("GEEKS");
        list.add("for");
        list.add("geeks");
  
        // Generate an iterator to iterate List in reverse
        // order
        ListIterator<String> gfg_itr
            = list.listIterator(list.size());
  
        // hasPrevious() returns true if the list has
        // previous element
        while (gfg_itr.hasPrevious()) 
        {
            // Iterate in reverse
            System.out.println(gfg_itr.previous());
        }
  
        // print list in Reverse using for loop
        for (int i = list.size() - 1; i >= 0; i--)
        {
            // access elements by their index (position)
            System.out.println(list.get(i));
        }
    }
}


Output

geeks
for
GEEKS
geeks
for
GEEKS

Approach 2: Using IntStream range(int startInclusive, int endExclusive)

  • This returns a sequential ordered IntStream from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1. It correctly handles overflow.

Syntax : 

static IntStream range(int startInclusive,   int endExclusive)

Parameters :

  • IntStream : A sequence of primitive int-valued elements.
  • startInclusive : The inclusive initial value.
  • endExclusive : The exclusive upper bound.

Java




// Java Program to iterate List in reverse order
  
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    public static void main(String[] args)
    {
  
        // For ArrayList
        List<Integer> list_li = new ArrayList<Integer>();
  
        // Add elements to list
        list_li.add(1);
        list_li.add(2);
        list_li.add(3);
        list_li.add(4);
        list_li.add(5);
  
        // Creating an IntStream
        IntStream stream = IntStream.range(0, list_li.size());
  
        // Displaying the elements in range
        // including the lower bound but
        // excluding the upper bound
        stream.map(i -> list_li.size() - i - 1).map(list_li::get)
            .forEach(System.out::println);
    }
}


Output

5
4
3
2
1

Note: IntStream range(int startInclusive, int endExclusive) basically works like a for loop. An equivalent sequence of increasing values can be produced sequentially as :

for (int i = startInclusive; i < endExclusive ; i++) 
{
 ...
 ...
 ...
}


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads