Open In App

next() Method in Java Collections

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

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

Java
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

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();

        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.
1
output

Step 2: After Fetching “Apple”

  • Cursor lies between Apple and Banana.
  • hasNext() -> true.
  • Calling next() returns "Banana" and moves the cursor after Banana.
2
output

Step 3: After Fetching “Banana”

  • Cursor lies between Banana and Cherry.
  • hasNext() -> true.
  • Calling next() returns "Cherry" and moves the cursor after Cherry.
3-
output

Step 4: After Fetching “Cherry”

  • Cursor now lies after the last element.
  • hasNext() -> false.
  • Iteration ends because no more elements exist.
5
output

Iterator.next() vs ListIterator.next()

FeatureIterator.next()ListIterator.next()
DirectionForward onlyForward and backward (with previous())
InterfaceIteratorListIterator
Applicable ToAll collectionsOnly list-based collections
Cursor PositionMoves forwardMoves forward and can move back

Explore