Example usage for java.util Collection toArray

List of usage examples for java.util Collection toArray

Introduction

In this page you can find the example usage for java.util Collection toArray.

Prototype

default <T> T[] toArray(IntFunction<T[]> generator) 

Source Link

Document

Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.

Usage

From source file:Main.java

public static String[] toStringArray(Collection<String> collection) {
    if (collection == null) {
        return null;
    }/* w  w  w .ja  va2 s . co m*/
    return collection.toArray(new String[collection.size()]);
}

From source file:com.shazam.fork.summary.OutcomeAggregator.java

private static Boolean and(final Collection<Boolean> booleans) {
    return BooleanUtils.and(booleans.toArray(new Boolean[booleans.size()]));
}

From source file:Main.java

public static String join(Collection<?> collection, String seperator) {
    Object[] objs = new Object[collection.size()];
    collection.toArray(objs);
    return join(objs, seperator);
}

From source file:Main.java

public static <T> T[] toArray(Collection<T> list, IntFunction<T[]> action) {
    return list.toArray(action.apply(list.size()));
}

From source file:Main.java

/**
 * Converts the <code>collection</code> into an array
 * using the <code>generator</code> to create the array
 * without creating a <code>Stream</code> on the collection.
 *
 * @param collection The collection to be converted
 * @param generator The generator to create the empty array
 * @param <T> The type of the elements
 *
 * @return An array with the length of the <code>collection</code>
 * containing all elements of the <code>collection</code>
 *
 * @throws NullPointerException if either <code>collection</code>
 * or <code>generator</code> is <code>null</code>
 *//* ww  w. j  a  v  a2  s  .com*/
public static <T> T[] toArray(Collection<T> collection, IntFunction<T[]> generator)
        throws NullPointerException {
    return collection.toArray(generator.apply(collection.size()));
}

From source file:Main.java

/**
 * Converts Collection to array. /*from w  ww  .j av  a  2 s . c  om*/
 * @param <T>
 * @param col
 * @param cls
 * @return 
 * @see java.util.Collection#toArray(T[]) 
 */
public static <T> T[] toArray(Collection<T> col, Class<T> cls) {
    T[] arr = (T[]) Array.newInstance(cls, col.size());
    return col.toArray(arr);
}

From source file:Main.java

public static String[] toArray(Collection<String> strings) {
    if (strings.isEmpty()) {
        return EMPTY_STRING_ARRAY;
    }//  w w  w. j av a  2 s .co  m
    return strings.toArray(new String[strings.size()]);
}

From source file:Main.java

/**
 * Copy the given Collection into a Class array.
 * The Collection must contain Class elements only.
 *
 * @param collection the Collection to copy
 * @return the Class array ({@code null} if the passed-in
 * Collection was {@code null})//from  w ww.  j  a  va  2s. co  m
 */
public static Class<?>[] toClassArray(Collection<Class<?>> collection) {
    if (collection == null) {
        return null;
    }
    return collection.toArray(new Class<?>[collection.size()]);
}

From source file:Main.java

/**
 * Converts a collection into an array.//from  w  ww .  j  a va2  s  . c  o  m
 * @param collection the collection to convert
 * @param clazz the class of the items in the collections
 * @return a non-null array
 */
public static <T> T[] convertToArray(Collection<T> collection, Class<T> clazz) {
    T[] result = (T[]) Array.newInstance(clazz, collection.size());
    return collection.toArray(result);
}

From source file:Main.java

/**
 * Copy the given Collection into a String array.
 * The Collection must contain String elements only.
 * @param collection the Collection to copy
 * @return the String array ({@code null} if the passed-in
 * Collection was {@code null})//  w w  w  .j  ava  2s  .co m
 */
public static String[] toStringArray(Collection<String> collection) {
    if (collection == null) {
        return null;
    }
    return collection.toArray(new String[collection.size()]);
}