Close

Java - How to convert Iterator To List?

[Updated: Apr 24, 2020, Created: Apr 24, 2020]

Java Collections Java 

This tutorial shows different ways to convert java.util.Iterator to java.util.List.

Using Iterator#hasNext() and Iterator#next()

package com.logicbig.example;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.IntStream;

public class IteratorToListExample {

  public static void main(String[] args) {
      Iterator<Integer> iterator = IntStream.range(1, 4).iterator();
      List<Integer> list = iteratorToList(iterator);
      System.out.println(list);
  }

  public static <T> List<T> iteratorToList(Iterator<T> iterator) {
      List<T> list = new ArrayList<>();
      while (iterator.hasNext()) {
          list.add(iterator.next());
      }
      return list;
  }
}
[1, 2, 3]

Using Iterator#forEachRemaining(list::add)

package com.logicbig.example;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.IntStream;

public class IteratorToListExample2 {

  public static void main(String[] args) {
      Iterator<Integer> iterator = IntStream.range(1, 4).iterator();
      List<Integer> list = iteratorToList(iterator);
      System.out.println(list);
  }

  public static <T> List<T> iteratorToList(Iterator<T> iterator) {
      List<T> list = new ArrayList<>();
      iterator.forEachRemaining(list::add);
      return list;
  }
}
[1, 2, 3]

Stream#collect(Collectors.toList())

package com.logicbig.example;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;

public class IteratorToListExample3 {

  public static void main(String[] args) {
      Iterator<Integer> iterator = IntStream.range(1, 4).iterator();
      List<Integer> list = iteratorToList(iterator);
      System.out.println(list);
  }

  public static <T> List<T> iteratorToList(Iterator<T> iterator) {
      Iterable<T> iterable = ()-> iterator;
      return StreamSupport.stream(iterable.spliterator(), false)
                          .collect(Collectors.toList());
  }
}
[1, 2, 3]
package com.logicbig.example;

import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;

public class IteratorToListExample4 {

  public static void main(String[] args) {
      Iterator<Integer> iterator = IntStream.range(1, 4).iterator();
      List<Integer> list = iteratorToList(iterator);
      System.out.println(list);
  }

  public static <T> List<T> iteratorToList(Iterator<T> iterator) {
      Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED);
      return StreamSupport.stream(spliterator, false)
                          .collect(Collectors.toList());
  }
}
[1, 2, 3]

Using Apache Commons Collections

pom.xml

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-collections4</artifactId>
   <version>4.1</version>
</dependency>
package com.logicbig.example;

import org.apache.commons.collections4.IteratorUtils;
import java.util.Iterator;
import java.util.List;
import java.util.stream.IntStream;

public class IteratorToListExample5 {

  public static void main(String[] args) {
      Iterator<Integer> iterator = IntStream.range(1, 4).iterator();
      List<Integer> list = IteratorUtils.toList(iterator);
      System.out.println(list);
  }
}
[1, 2, 3]

See Also