Convert an Iterator to a List in Java - GeeksforGeeks

11 Dec, 2018
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:Get the Iterator.Create an empty list.Add each element of the iterator to the list using forEachRemaining() method.Return the list.Below is the implementation of the above approach:// 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);    }}Output:[1, 2, 3, 4, 5]

Get the Iterator.Create an empty list.Add each element of the iterator to the list using forEachRemaining() method.Return the list.
Get the Iterator.
Create an empty list.
Add each element of the iterator to the list using forEachRemaining() method.
Return the list.
Below is the implementation of the above approach:
// 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);    }}
[1, 2, 3, 4, 5]

Using Iterable as intermediate:Get the Iterator.Convert the iterator to iterable using lambda expression.Convert the iterable to list using Stream.Return the list.Below is the implementation of the above approach:// 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);    }}Output:[1, 2, 3, 4, 5]

Get the Iterator.Convert the iterator to iterable using lambda expression.Convert the iterable to list using Stream.Return the list.
Get the Iterator.
Convert the iterator to iterable using lambda expression.
Convert the iterable to list using Stream.
Return the list.
Below is the implementation of the above approach:
// 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);    }}
[1, 2, 3, 4, 5]

Java - util package
Java-Iterator
java-list
Java-List-Programs
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
Arrays.sort() in Java with examples
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
How to iterate any Map in Java
Initialize an ArrayList in Java
Interfaces in Java