The Iterator and ListIterator interfaces provide the next method. It is used to retrieve the next element in a collection while traversing through it. It returns the next element in the iteration and moves the cursor forward by one position.
Syntax
public E next()
- Return type: Same as collection, such as ArrayList, LinkedList, etc.
- Return value: The next element in the iteration.
- Exception: Throws NoSuchElementException if the iteration has no more elements.
Example 1: Using next() in Iterator
import java.util.*;
public class GFG{
public static void main(String[] args) {
List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry");
Iterator<String> iterator = fruits.iterator();
// checks if element exists
while (iterator.hasNext()){
// fetches next element
String fruit = iterator.next();
System.out.println(fruit);
}
}
}
Output
Apple Banana Cherry
Explanation
- The iterator() method returns an Iterator for the list.
- The hasNext() method checks if another element exists.
- The next() method retrieves the next element and advances the cursor.
- The loop continues until no elements remain.
Example 2: Using next() in ListIterator
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();
System.out.println("Forward Traversal:");
while (listItr.hasNext()) {
System.out.println(listItr.next());
}
}
}
Output
Forward Traversal: Dog Cat Elephant
Explanation
- The listIterator() method returns a ListIterator for the list.
- The hasNext() method checks if another element exists in the list.
- The next() method retrieves the next element and moves the cursor forward.
- The loop continues until all elements in the list are traversed.
Working of next() Method
The Iterator cursor does not point directly to an element, but rather lies between elements. To understand how next() works, let’s take a list containing “Apple”, “Banana”, and “Cherry”.
Step 1: Before Iteration
- The cursor lies before the first element.
- hasNext() -> true (since the next element exists).
- Calling next() returns "Apple" and moves the cursor after Apple.

Step 2: After Fetching “Apple”
- Cursor lies between Apple and Banana.
- hasNext() -> true.
- Calling next() returns "Banana" and moves the cursor after Banana.

Step 3: After Fetching “Banana”
- Cursor lies between Banana and Cherry.
- hasNext() -> true.
- Calling next() returns "Cherry" and moves the cursor after Cherry.

Step 4: After Fetching “Cherry”
- Cursor now lies after the last element.
- hasNext() -> false.
- Iteration ends because no more elements exist.

Iterator.next() vs ListIterator.next()
| Feature | Iterator.next() | ListIterator.next() |
|---|---|---|
| Direction | Forward only | Forward and backward (with previous()) |
| Interface | Iterator | ListIterator |
| Applicable To | All collections | Only list-based collections |
| Cursor Position | Moves forward | Moves forward and can move back |