Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 510
Laurent Grégoire Avatar asked Sep 16 '25 06:09

Laurent Grégoire


1 Answers

You can use this:

 StreamSupport.stream(spliterator, false).collect(Collectors.toList())
like image 95
Nir Levy Avatar answered Sep 17 '25 21:09

Nir Levy