Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Iterable to Array using Guava

Tags:

I need to return a String array. I use Guava to split the String. Please see the code below

Iterable<String> arrayOfValues =  Splitter.on(";").split(myString); 

it returns an Iterable. but i need a String[]. Is there are any way to give Iterator< Element > and convert that to Array[]. Many Thanks

like image 303
stacktome Avatar asked Jul 31 '13 10:07

stacktome


People also ask

How do you make an array iterable?

To make an array iterable either you need to convert it to a stream or as a list using the asList() or stream() methods respectively. Then you can get an iterator for these objects using the iterator() method.

What does Iterables partition do?

partition. Divides an iterable into unmodifiable sublists of the given size (the final iterable may be smaller).


2 Answers

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

like image 66
Narendra Pathai Avatar answered Oct 14 '22 19:10

Narendra Pathai


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.

like image 22
rec Avatar answered Oct 14 '22 19:10

rec