Convert an Iterator to a List in Java

Given an Iterator, the task is to convert it into List in Java.

Examples:

Input: Iterator = {1, 2, 3, 4, 5}
Output: {1, 2, 3, 4, 5}

Input: Iterator = {'G', 'e', 'e', 'k', 's'}
Output: {'G', 'e', 'e', 'k', 's'}

Below are the various ways to do so:

  • Naive Approach:
    1. Get the Iterator.
    2. Create an empty list.
    3. Add each element of the iterator to the list using forEachRemaining() method.
    4. Return the list.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to get a List
    // from a given Iterator
      
    import java.util.*;
      
    class GFG {
      
        // Function to get the List
        public static <T> List<T>
        getListFromIterator(Iterator<T> iterator)
        {
      
            // Create an empty list
            List<T> list = new ArrayList<>();
      
            // Add each element of iterator to the List
            iterator.forEachRemaining(list::add);
      
            // Return the List
            return list;
        }
      
        // Driver code
        public static void main(String[] args)
        {
      
            // Get the Iterator
            Iterator<Integer>
                iterator = Arrays.asList(1, 2, 3, 4, 5)
                               .iterator();
      
            // Get the List from the Iterator
            List<Integer>
                list = getListFromIterator(iterator);
      
            // Print the list
            System.out.println(list);
        }
    }

    chevron_right

    
    

    Output:

    [1, 2, 3, 4, 5]
    
  • Using Iterable as intermediate:
    1. Get the Iterator.
    2. Convert the iterator to iterable using lambda expression.
    3. Convert the iterable to list using Stream.
    4. Return the list.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to get a List
    // from a given Iterator
      
    import java.util.*;
    import java.util.stream.Collectors;
    import java.util.stream.StreamSupport;
      
    class GFG {
      
        // Function to get the List
        public static <T> List<T>
        getListFromIterator(Iterator<T> iterator)
        {
      
            // Convert iterator to iterable
            Iterable<T> iterable = () -> iterator;
      
            // Create a List from the Iterable
            List<T> list = StreamSupport
                               .stream(iterable.spliterator(), false)
                               .collect(Collectors.toList());
      
            // Return the List
            return list;
        }
      
        // Driver code
        public static void main(String[] args)
        {
      
            // Get the Iterator
            Iterator<Integer>
                iterator = Arrays.asList(1, 2, 3, 4, 5)
                               .iterator();
      
            // Get the List from the Iterator
            List<Integer>
                list = getListFromIterator(iterator);
      
            // Print the list
            System.out.println(list);
        }
    }

    chevron_right

    
    

    Output:

    [1, 2, 3, 4, 5]
    


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.