Convert an Iterator to a List in Java

Last Updated : 2 Sep, 2026

An Iterator is used to traverse elements of a collection one by one. Java does not provide a direct Iterator-to-List conversion method, but we can easily create a List by consuming the elements of the Iterator.

  • forEachRemaining() provides a simple way to add all remaining elements to a List.
  • An Iterator is consumed during conversion, so it cannot be used again to retrieve the same elements.

Example:

Input: Iterator = [10, 20, 30, 40, 50]
Output: List = [10, 20, 30, 40, 50]

Ways to Convert Iterator to List

1. Using forEachRemaining()

The forEachRemaining() method performs an action for every element that remains in the Iterator. We can use it to add each element directly to an ArrayList.

Approach:

  1. Create an Iterator.
  2. Create an empty ArrayList.
  3. Use forEachRemaining() to add the elements to the List.
  4. Print the resulting List.
Java
import java.util.*;

class IteratorToList {
    public static <T> List<T> convertToList(Iterator<T> iterator) {

        List<T> list = new ArrayList<>();

        iterator.forEachRemaining(list::add);

        return list;
    }

    public static void main(String[] args) {

        Iterator<Integer> iterator =
                Arrays.asList(10, 20, 30, 40, 50).iterator();

        List<Integer> list = convertToList(iterator);

        System.out.println("List: " + list);
    }
}

Output
List: [10, 20, 30, 40, 50]

Explanation: The forEachRemaining() method visits every remaining element of the Iterator and list::add adds each element to the ArrayList.

2. Using Iterable and Stream

An Iterator can first be wrapped in an Iterable using a lambda expression. The resulting Iterable can then be converted into a Stream using StreamSupport.

Approach:

  1. Create an Iterator.
  2. Convert the Iterator into an Iterable.
  3. Create a sequential Stream from the Iterable.
  4. Collect the elements into a List.
Java
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

class IteratorToListStream {

    public static <T> List<T>
    convertToList(Iterator<T> iterator)
    {

        Iterable<T> iterable = () -> iterator;

        return StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList());
    }

    public static void main(String[] args)
    {

        Iterator<String> iterator = Arrays.asList("Java", "Python", "C++", "Go").iterator();

        List<String> list = convertToList(iterator);

        System.out.println("List: " + list);
    }
}

Output
List: [Java, Python, C++, Go]

Explanation: The Iterator is wrapped as an Iterable, which allows StreamSupport to create a Stream. The Stream then collects all elements into a List.

Note: Both approaches consume the Iterator. After conversion, calling next() on the same Iterator will not return the elements that have already been processed.

Comment