Previous() Method in Java Collections

Last Updated : 28 Oct, 2025

The previous() method in Java is a part of the ListIterator interface, which allows bidirectional traversal of elements in a list. It moves the cursor one step backward in the list and returns the previous element in the sequence.

Syntax

public E previous()

  • Parameters: No parameters.
  • Return Type: Same type as list elements (e.g., String, Integer).
  • Return Value: Returns the previous element and moves the cursor one step backward.

Example: Using previous() in ListIterator

Java
import java.util.*;

public class GFG {

    public static void main(String[] args)
    {

        List<String> animals
            = Arrays.asList("Dog", "Cat", "Elephant");
        ListIterator<String> listItr
            = animals.listIterator(animals.size());

        System.out.println("Backward Traversal:");
        while (listItr.hasPrevious()) {
            System.out.println(listItr.previous());
        }
    }
}

Output
Backward Traversal:
Elephant
Cat
Dog

Explanation

  • The listIterator(animals.size()) method positions the cursor after the last element for backward traversal.
  • The hasPrevious() method checks if a previous element exists in the list.
  • The previous() method returns the previous element and moves the cursor one step backward.
  • The loop continues until the cursor reaches the beginning of the list.

Working of previous() Method

The previous() method retrieves the element that exists before the current cursor position and then moves the cursor backward. To understand how previous() works, let’s take a list containing “Apple”, “Banana”, and “Cherry”.

Step 1: After Forward Traversal

  • Cursor lies after the last element.
  • hasPrevious() -> returns true (since "Cherry" exists behind).
  • Calling previous() returns "Cherry" and moves the cursor before Cherry.
4-
output

Step 2: After Fetching “Cherry”

  • Cursor lies between Banana and Cherry.
  • hasPrevious() -> returns true (since "Banana" exists behind).
  • Calling previous() returns "Banana" and moves the cursor before Banana.
6
output

Step 3: After Fetching “Banana”

  • Cursor lies between Apple and Banana.
  • hasPrevious() -> returns true (since "Apple" exists behind).
  • Calling previous() returns "Apple" and moves the cursor before Apple.
7
output

Step 4: After Fetching “Apple”

  • Cursor lies before the first element.
  • hasPrevious() -> returns false (no elements behind).
  • Backward iteration ends because the start of the list is reached.
8
output

previous() vs next()

Featurenext()previous()
DirectionMoves forward in the listMoves backward in the list
InterfaceAvailable in Iterator and ListIteratorAvailable only in ListIterator
Applicable ToAll collectionsOnly list-based collections
Cursor MovementMoves cursor one step forwardMoves cursor one step backward
Comment