Open In App

hasPrevious() Method in Java Collections

Last Updated : 28 Oct, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The hasPrevious() method is provided by the ListIterator interface in Java. It is used to check whether there is a previous element while traversing a list in the reverse direction. It allows bi-directional iteration, moving both forward and backward through a list.

Syntax

public boolean hasPrevious()

Return type: boolean
Return value:

  • true -> if there is a previous element in the list.
  • false -> if the cursor is at the beginning of the list (no previous element).

Exception: This method does not throw any exception; it only checks the existence of a previous element.

Example: Using hasPrevious() 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();

        // Move cursor to the end for backward traversal
        while (listItr.hasNext()) {
            listItr.next();
        }

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

Output
Backward Traversal:
Elephant
Cat
Dog

Explanation

  • The listIterator() method returns a ListIterator for the list.
  • The first while loop uses hasNext() and next() to move the cursor after the last element.
  • The second loop uses hasPrevious() and previous() to traverse the list in reverse order.
  • The loop continues until hasPrevious() returns false, indicating the start of the list has been reached.

Working of hasPrevious() Method

The hasPrevious() method checks if there is an element before the current cursor position. It works together with the previous() method to move the cursor backward through the list. To understand how hasprevious() works, let’s take a list containing “Apple”, “Banana”, and “Cherry”.

Step 1: After Forward Traversal

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

Step 2: After Fetching “Cherry”

  • Cursor lies between Banana and Cherry
  • hasPrevious() -> Return 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() -> Return 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() -> false (no elements behind).
  • Backward iteration ends because the beginning of the list is reached.
8
output

hasPrevious() vs hasNext()

FeaturehasNext()hasPrevious()
DirectionChecks for next element (forward)Checks for previous element (backward)
InterfaceAvailable in Iterator and ListIteratorAvailable only in ListIterator
Applicable ToAll collectionsOnly list-based collections
Cursor MovementMoves forward with next()Moves backward with previous()



Explore