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

1.2k Views Asked by At

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?

1

There are 1 best solutions below

0
Nir Levy On BEST ANSWER

You can use this:

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