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

ObjectconvertToArray(Collection collection)
convert To Array
Iterator iter = collection.iterator();
if (!iter.hasNext())
    return null;
Class objCls = iter.next().getClass();
if (objCls == Integer.class) {
    return convertToIntArray(collection);
} else if (objCls == Long.class) {
    return convertToLongArray(collection);
...
double[]convertToArray(Collection input)
convert To Array
double[] result = new double[input.size()];
int count = 0;
for (double d : input) {
    result[count++] = d;
return result;
T[]convertToArray(Collection items)
convert To Array
return (T[]) items.toArray();
String[]toArray(Collection c)
Returns a string array contains all the String object in the Collection c.
if (c == null || c.size() == 0) {
    return EMPTY_STRING_ARRAY;
String[] result = new String[c.size()];
int i = 0;
for (Iterator iter = c.iterator(); iter.hasNext();) {
    result[i++] = (String) iter.next();
return result;
int[]toArray(Collection coll)
to Array
int[] target = new int[coll.size()];
int pos = 0;
for (int i : coll)
    target[pos++] = i;
return target;
byte[]toArray(Collection bytes)
Converts a Collection of Number to a byte array.
byte[] array = new byte[bytes.size()];
Iterator<? extends Number> iter = bytes.iterator();
for (int i = 0; i < bytes.size(); i++) {
    array[i] = iter.next().byteValue();
return array;
T[]toArray(Collection c, T[] arr)
Copies all elements from collection to array and asserts that array is big enough to hold the collection.
T[] a = c.toArray(arr);
assert a == arr;
return arr;
Object[]toArray(Collection collection)
to Array
return collection.toArray(new Object[collection.size()]);
Object[][]toArray(Collection tests)
to Array
Object[][] result = new Object[tests.size()][];
int i = 0;
for (Object testCase : tests) {
    result[i++] = new Object[] { testCase };
return result;
boolean[]toArray(Collection list)
Converts a collection ( List , Set ...) of Boolean objects to a boolean[] .
return asPrimitiveArray(list.toArray(new Boolean[list.size()]));