22

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

Oliver Watkins
  • 10,301
  • 19
  • 83
  • 168
stacktome
  • 791
  • 2
  • 8
  • 31

8 Answers8

39

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

Isaac Kwan
  • 99
  • 2
  • 10
Narendra Pathai
  • 38,384
  • 18
  • 73
  • 117
  • 6
    Iterables#toArray is not public – guai Aug 27 '15 at 08:46
  • do you know if is there a single code like like this but without guava? – Aquarius Power Dec 28 '15 at 17:50
  • 3
    @guai Just for benefit of others, Iterables.toArray without second parameter for type class is private. The method above with this parameter is public. – Breeno Feb 15 '17 at 20:57
  • The conversion to arr [] is super slow. It takes away all the speed benefit you got.. for ex a 500k split loop (~300 values) which finished in 50ms finishes in about 6000ms when converted to arr [] – Nrj Jun 26 '18 at 18:00
22

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.

rec
  • 8,872
  • 3
  • 24
  • 41
7

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

StreamSupport.stream(yourIterable.spliterator(), false).toArray(String[]::new)
Andy Brown
  • 8,676
  • 2
  • 30
  • 49
4

You can also use Guava's FluentIterable :

FluentIterable.from(Splitter.on(":").split(mystring)).toArray(String.class)
Oussama Zoghlami
  • 1,576
  • 16
  • 23
2

Without Guava, you could convert an Iterable<String> to an array with something like this

public static <String> String[] toArray(Iterable<String> itr) {
    ArrayList<String> ret = new ArrayList<>();
    for(String t : itr) {
        ret.add(t);
    }
    return ret.toArray(new String[]);
}

It’s a bit hard to write a generic version of this function due to the myriad issues with generic arrays in Java, so tread lightly.

9999years
  • 1,263
  • 11
  • 13
0

In guava you can convert it as Follows

 String test= "Welcome to java world";
 Iterable<String> tokens=Splitter.on(" ").split(test);
 String array[]=Iterables.toArray(tokens,String.class);

If you just want to access the Iterator elements like an array then no need to convert. You can directly access these elements by index

e.g

 System.out.println(Iterables.get(tokens,2));
Sam
  • 477
  • 3
  • 8
0

Even though the question says "Convert Iterator to Array", answers only explain how to convert Iterable to Array.

So, to convert Iterator to Array with Guava you can use Iterators.toArray(Iterator<? extends T> iterator, Class<T> type).

Taylan
  • 2,675
  • 2
  • 25
  • 35
0

I truly agree with @rec's answer: Don't use fancy libraries if you don't need them.

But, I strongly recommend reading Guava's splitter documentation:

Because java builtin splitter may has some tricky behaviors..

For example, suppose you have trailing commas or successive commas in your input. What will be returned after split?

  • null
  • ""
  • (discarded)

;)

Vineeth Sai
  • 3,113
  • 7
  • 19
  • 28