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

double[]toArray(Collection values)
Returns an array representation
double[] result = new double[values.size()];
int i = 0;
for (Double value : values) {
    result[i] = value;
    i++;
return result;
double[]toArray(Collection values)
to Array
if (values == null) {
    throw new IllegalArgumentException("No null value allowed!");
double[] result = new double[values.size()];
int cnt = 0;
for (Double value : values) {
    result[cnt++] = value;
return result;
int[]toArray(Collection collection)
Transforms a Collection of Integer instances to a simple int array.
if (collection.isEmpty())
    return EMPTY_ARRAY;
int[] array = new int[collection.size()];
Iterator<Integer> iterator = collection.iterator();
for (int n = 0; iterator.hasNext(); ++n)
    array[n] = iterator.next().intValue();
return array;
int[]toArray(Collection collection)
Method toArray.
int[] ar = new int[collection.size()];
int i = 0;
for (Integer t : collection) {
    ar[i++] = t;
return ar;
long[]toArray(Collection collection, Long defaultValue)
to Array
if (collection == null)
    return new long[0];
long[] ret = new long[collection.size()];
int count = 0;
for (Long element : collection) {
    if (element == null) {
        if (defaultValue == null)
            continue;
...
String[]toArray(Collection col)
to Array
String[] array = new String[col.size()];
col.toArray(array);
return array;
StringtoArray(Collection collection)

Converts a collection of Strings into a Javascript array.

StringBuilder sb = new StringBuilder("[");
Iterator<String> it = collection.iterator();
while (it.hasNext()) {
    sb.append("'");
    sb.append(it.next());
    sb.append("'");
    if (it.hasNext()) {
        sb.append(",");
...
String[]toArray(Collection collection)
to Array
if (collection == null) {
    throw new IllegalArgumentException("argument must not be null.");
return collection.toArray(new String[collection.size()]);
String[]toArray(Collection list)
Shortcut to create a string array from a collection of strings
return list.toArray(new String[list.size()]);
String[]toArray(Collection stringCollection)
produces a String[] out of a String Collection.
String[] sArray = new String[stringCollection.size()];
int i = 0;
for (Iterator<String> iterator = stringCollection.iterator(); iterator.hasNext();) {
    sArray[i] = (String) iterator.next();
    i++;
return sArray;