Tip: Converting a Java List to an array

Monday Mar 29th 2021 by Octavia Andreea Anghel
Article Image

How to convert a Java List to an array.

Converting a Java List an array can be done like this:

Foo[] arr = givenList.toArray(new Foo[0]);

Or, like this:

Foo[] arr = new Foo[givenList.size()];

Or, in functional style:

Foo[] arr = givenList.stream().toArray(Foo[]::new);

Foo[] arr = givenList.toArray(Foo[]::new);
Mobile Site | Full Site