Convert Iterable to Array using Guava

In java 8 I prefer to use a stream to convert an iterable to an array:

StreamSupport.stream(yourIterable.spliterator(), false).toArray(String[]::new)

You can also use Guava's FluentIterable :

FluentIterable.from(Splitter.on(":").split(mystring)).toArray(String.class)

Use the Iterables.toArray(Iterable<? extends T> iterable, Class<T> type) method in Guava.


If you use the plain Java String.split(regex) method, you're fine. It returns a String[].

"my;string".split(";")

String[] splits = mystring.split(";");

Don't use fancy libraries if you don't need them.

Tags:

Java

Guava