javaspliterator

What is the best idiom to convert a spliterator to a list in Java?


I want to convert a Spliterator<T> into a List<T> in Java.

What is the best idiom to do that? I'm currently using the following code:

 List<T> list = new ArrayList<>();
 spliterator.forEachRemaining(list::add);

Is there a simpler / faster way?


Solution

  • You can use this:

     StreamSupport.stream(spliterator, false).collect(Collectors.toList())