
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert an Iterator to a List in Java
An Iterator is used for cycling through a Collection of objects. Whereas a List is an Interface which is used for storing an ordered collection of elements. Both classes are part of the Java Collections Framework.
Converting an Iterator to a List in Java
Sometimes we need to convert an Iterator to a collection. In this article, we will learn how to convert an Iterator to a List in Java using multiple ways. Those are:
- Using a while loop
- Using Iterator.forEachRemaining()
- Using Stream API
Let's understand each of them in detail.
Using a while Loop
In this method, we will use a while loop to iterate over the Iterator and add each element to a new List. We will use the method hasNext() to check if there are more elements in the Iterator. If there are more elements, we will use the method next() to get the next element and add it to the List.
The syntax of the hasNext() and next() methods is as follows -
public boolean hasNext() public E next() throws NoSuchElementException
Example
Following is the code to convert an Iterator to a List using a while loop -
import java.util.*; import java.io.*; public class IteratorToList { public static void main(String[] args) { // Create an Iterator object Iterator<String> iterator = Arrays.asList("A", "B", "C", "D", "E").iterator(); // Create a List object List<String> list = new ArrayList<>(); // Iterate over the Iterator and add each element to the List while(iterator.hasNext()) { list.add(iterator.next()); } // Print the List System.out.println("List: " + list); } }
Output
The output of the above code will be:
List: [A, B, C, D, E]
Using the Iterator.forEachRemaining() Method
In this method, we will use the Iterator.forEachRemaining() method to convert an Iterator to a List. The forEachRemaining() method is a default method in the Iterator interface. It performs the given action for each remaining element in the iteration until all elements have been processed or the action throws an exception.
The syntax of the forEachRemaining() method is as follows:
public default void forEachRemaining(Consumer<? super E> action)
Example
Following is the code to convert an Iterator to a List using the forEachRemaining() method:
import java.util.*; import java.io.*; import java.util.function.Consumer; public class IteratorToList { public static void main(String[] args) { // Create an Iterator object Iterator<String> iterator = Arrays.asList("A", "B", "C", "D", "E").iterator(); // Create a List object List<String> list = new ArrayList<>(); // Use forEachRemaining() method to add each element to the List iterator.forEachRemaining(new Consumer<String>() { public void accept(String element) { list.add(element); } }); // Print the List System.out.println("List: " + list); } }
Output
The output of the above code will be:
List: [A, B, C, D, E]
Using the Stream API
We will use the Stream API to convert an Iterator to a List. The Stream API is a new abstraction introduced in Java 8. It allows us to process sequences of elements in a functional style.
We will use the StreamSupport.stream() method to convert the Iterator to a Stream. Then we will use the collect() method to convert the Stream to a List. The syntax of the StreamSupport.stream() and collect() methods is as follows:
public static <T> Stream<T> stream(Iterator<T> iterator, boolean parallel) public final <R,A> R collect(Collector<? super T, A, R> collector)
Example
Following is the code to convert an Iterator to a List using the Stream API -
import java.util.*; import java.io.*; import java.util.stream.*; public class IteratorToList { public static void main(String[] args) { // Create an Iterator object Iterator<String> iterator = Arrays.asList("A", "B", "C", "D", "E").iterator(); // Convert the Iterator to a List using Stream API List<String> list = StreamSupport.stream(((Iterable<String>) () -> iterator).spliterator(), false) .collect(Collectors.toList()); // Print the List System.out.println("List: " + list); } }
Output
The output of the above code will be -
List: [A, B, C, D, E]