Convert an Iterator to a List in Java
This post will discuss how to convert an iterator to a list in Java.
1. Naive solution
A simple solution is to create an empty list and add each remaining element of the iterator to that list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; class Main { // Convert an Iterator to a List in Java 8 and above public static void main(String[] args) { Iterator<Integer> iterator = Arrays.asList(1, 2, 3, 4, 5).iterator(); List<Integer> mutableList = new ArrayList<>(); iterator.forEachRemaining(mutableList::add); // Java 8 and above System.out.println(mutableList); } } |
Output:
[1, 2, 3, 4, 5]
2. Converting the Iterator to an Iterable
Here, the idea is to convert the Iterator to an Iterable, which can be easily done by using lambda in Java 8 and above. Then we convert the iterable to stream using the StreamSupport.stream() method. Finally, we get a list from the stream using a collector.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; import java.util.stream.StreamSupport; class Main { // Convert an Iterator to a List in Java 8 and above public static void main(String[] args) { Iterator<Integer> iterator = Arrays.asList(1, 2, 3, 4, 5).iterator(); // Convert Iterator to Iterable Iterable<Integer> iterable = () -> iterator; List<Integer> mutableList = StreamSupport .stream(iterable.spliterator(), false) .collect(Collectors.toList()); System.out.println(mutableList); } } |
Output:
[1, 2, 3, 4, 5]
3. Using Guava Library
Guava library provides several utility methods to get a mutable or an immutable list from an iterator, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import java.util.Arrays; import java.util.Iterator; import java.util.List; class Main { // Convert an Iterator to a list in Java public static void main(String[] args) { // 1. Convert Iterator to a mutable list in Java Iterator<Integer> itr = Arrays.asList(1, 2, 3, 4, 5).iterator(); List<Integer> mutableList = Lists.newArrayList(itr); System.out.println(mutableList); // 2. Convert Iterator to an immutable list in Java Iterator<String> it = Arrays.asList("A", "B", "C").iterator(); List<String> immutableList = ImmutableList.copyOf(it); System.out.println(immutableList); } } |
Output:
[1, 2, 3, 4, 5]
[A, B, C]
4. Using Apache Commons Collections
Similar to Guava library, Apache Commons Collections also provides static utility methods and decorators for Iterator instances. To serve our purpose, we can use the IteratorUtils.toList() method to get a list based on an iterator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import org.apache.commons.collections4.IteratorUtils; import java.util.Arrays; import java.util.Iterator; import java.util.List; class Main { // Convert an Iterator to a List in Java public static void main(String[] args) { Iterator<Integer> iterator = Arrays.asList(1, 2, 3, 4, 5).iterator(); List<Integer> mutableList = IteratorUtils.toList(iterator); System.out.println(mutableList); } } |
Output:
[1, 2, 3, 4, 5]
That’s all about converting an Iterator to a List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)