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

Object[]asArray(Collection c)
Returns an array with the elements in a given collection.
Object[] array = new Object[c.size()];
int i = 0;
for (Iterator it = c.iterator(); it.hasNext();) {
    array[i++] = it.next();
return array;
E[]asArray(Collection collection, E[] a)
as Array
return asList(collection).toArray(a);
boolean[]asBooleanArray(Collection collection)
as Boolean Array
boolean[] array = new boolean[collection.size()];
Iterator<Boolean> iterator = collection.iterator();
int i = 0;
while (iterator.hasNext()) {
    array[i++] = iterator.next();
return array;
long[]asLongArray(final Collection ls)
as Long Array
final long[] d = new long[ls.size()];
int i = 0;
for (final Number num : ls)
    d[i++] = num.longValue();
return d;
long[]asLongArray(java.util.Collection c)
Returns 'c' as a long array.
int i = 0, n = (c == null) ? 0 : c.size();
long[] ans = new long[n];
if (n > 0)
    for (Number nr : c)
        ans[i++] = nr.longValue();
return ans;
Object[]collectionToArray(Collection collection)
collection To Array
Object[] objs = null;
if (isNotEmpty(collection)) {
    objs = new Object[collection.size()];
    collection.toArray(objs);
return objs;
int[]collectionToIntArray(Collection c)
collection To Int Array
int[] toReturn = new int[c.size()];
Iterator<Integer> it = c.iterator();
for (int i = 0; i < toReturn.length; i++) {
    toReturn[i] = it.next().intValue();
return toReturn;
int[]collectionToIntArray(final Collection numbers)
collection To Int Array
final int[] ret = new int[numbers.size()];
int i = 0;
for (Number n : numbers) {
    ret[i++] = n.intValue();
return ret;
Object[]collectionToObjectArray(Collection c)
Convenience method for converting a collection to an Object[]
if (c == null)
    return new Object[0];
return c.toArray(new Object[c.size()]);
CharSequencecollectionToObjectArray(final Collection _list)
collection To Object Array
final StringBuilder ret = new StringBuilder();
if (!_list.isEmpty()) {
    boolean first = true;
    ret.append("[");
    for (final CharSequence entry : _list) {
        if (first) {
            first = false;
        } else {
...