hasPrevious() Method in Java Collections
Last Updated :
28 Oct, 2025
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());
}
}
}
OutputBackward 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.
outputStep 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.
outputStep 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.
outputStep 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.
outputhasPrevious() vs hasNext()
| Feature | hasNext() | hasPrevious() |
|---|
| Direction | Checks for next element (forward) | Checks for previous element (backward) |
| Interface | Available in Iterator and ListIterator | Available only in ListIterator |
| Applicable To | All collections | Only list-based collections |
| Cursor Movement | Moves forward with next() | Moves backward with previous() |
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java