33

What I'm looking for is a generic version of Object[] java.util.Collection.toArray() or a less verbose alternative to using T[] java.util.Collection.toArray(T[] array). I can currently write:

Collection<String> strings;
String[] array = strings.toArray(new String[strings.size()]);

What I'm looking for is something like:

@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<T> collection, Class<T> clazz) {
    return collection.toArray((T[]) Array.newInstance(clazz, collection.size()));
}

which I can then use as:

String[] array = Util.toArray(strings, String.class);

So is anything like this implemented in Guava or in Commons Collections?

Of course I can write my own (the above), which seems to be as fast as toArray(T[] array).

Mihai
  • 1,261
  • 2
  • 12
  • 19
  • So basically, you're just not wanting to have to put in the `array` parameter, but you're happy to put a class name for a static method and a class literal instead? – chrylis -cautiouslyoptimistic- Feb 12 '14 at 13:50
  • The fact that you want to cut down a number of characters equal to `strings.size()` in length would imply that you are carrying this conversion rather a lot. So then the answer is, do it less. – Boris the Spider Feb 12 '14 at 13:54
  • You're worried because 8 more characters is a lot? – Olivier Grégoire Feb 12 '14 at 16:22
  • I too wish I could use it less. More importantly, the `strings.size()` version is less readable and more error prone (must keep the two `strings` references in sync when modifying) than `Iterables.toArray()`. Even more, now I can write `Iterables.toArray(fooReturningArray(), String.class)`. – Mihai Feb 12 '14 at 16:46

2 Answers2

47

Iterables.toArray() from Guava.

Pang
  • 9,564
  • 146
  • 81
  • 122
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • `Iterables.toArray(strings, String.class);` because toArray with one parameter is package private. – mkczyk Apr 29 '20 at 11:18
12

You can shorten it with

String[] array = strings.toArray(new String[0]);

which also happens to be more efficient.

With Java 8 you can also use this, but it seems unnecessarily complicated and is probably slower:

String[] array = strings.stream().toArray(String[]::new);     // Java 8
assylias
  • 321,522
  • 82
  • 660
  • 783
  • Thank you. However, I can't use Java 8 nor do I want to needlessly create objects on memory constrained devices (android) with `strings.toArray(new String[0])`. Plus, these solutions aren't much more readable than `strings.toArray(new String[strings.size()])`. – Mihai Feb 12 '14 at 16:34
  • 4
    If those 'new String[0]'s are making any difference to your performance at all, congratulations, you must have one screaming fast app. – Kevin Bourrillion Feb 12 '14 at 16:37
  • 2
    `new String[0]` will actually be faster: https://shipilev.net/blog/2016/arrays-wisdom-ancients/ – Karol S Apr 05 '17 at 23:44
  • @KarolS Absolutely, amended. I discovered that later: http://stackoverflow.com/a/29444594/829571 – assylias Apr 06 '17 at 15:51