Java Utililty Methods Collection to Array

List of utility methods to do Collection to Array

Description

The list of methods to do Collection to Array are organized into topic(s).

Method

String[]toArray(Collection strings)
to Array
if (strings.isEmpty()) {
    return EMPTY_STRING_ARRAY;
return strings.toArray(new String[strings.size()]);
String[]toArray(Collection values)
to Array
return values.toArray(EMPTY_ARRAY);
T[]toArray(Collection c, T[] sample)
This is a replacement for Collection#toArray(Object[]) .
final int size = c.size();
if (size == sample.length && size < ARRAY_COPY_THRESHOLD) {
    int i = 0;
    for (T t : c) {
        sample[i++] = t;
    return sample;
return c.toArray(sample);
T[]toArray(Collection collection)
Cast a collection to an array
return (T[]) collection.toArray(new Object[collection.size()]);
T[]toArray(Collection list, Object[] type)
Convenience method for List#toArray(Object[]) to pass compiler warnings for generic typed array.
Object[] array = list.toArray(type);
return (T[]) array;
Object[]toArray(final Collection c)
to Array
return c == null ? null : c.toArray();
byte[]toArray(final Collection coll, byte[] array)
to Array
if (array.length < coll.size()) {
    array = new byte[coll.size()];
final Iterator<Byte> it = coll.iterator();
int i = 0;
while (it.hasNext()) {
    array[i++] = it.next().byteValue();
return array;
int[]toArray(final Collection collection)
to Array
if (collection == null || collection.isEmpty()) {
    return new int[0];
final int[] result = new int[collection.size()];
int index = 0;
for (final Integer integer : collection) {
    result[index] = integer.intValue();
    index++;
...
String[]toArray(final Collection collection)
to Array
if (collection == null)
    return null;
final String[] a = new String[collection.size()];
collection.toArray(a);
return a;
String[]toArray(java.util.Collection collection)
to Array
String[] array = new String[collection.size()];
Iterator<String> i = collection.iterator();
int index = 0;
while (i.hasNext()) {
    array[index] = i.next();
    index++;
return array;
...