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:net.firejack.platform.core.utils.ArrayUtils.java

public static <T> T[] getArray(Collection<T> itemList, Class<T> clazz) {
    return itemList == null || itemList.isEmpty() ? null
            : itemList.toArray((T[]) Array.newInstance(clazz, itemList.size()));
}

From source file:Main.java

public static File[] listFiles(File directory, boolean recurse, FileFilter filter) {
    if (!recurse) {
        return directory.listFiles(filter);
    } else {/*from www. ja  v  a  2s. c om*/
        Collection<File> mFiles = new java.util.LinkedList<>();
        innerListFiles(mFiles, directory, filter);
        return mFiles.toArray(new File[mFiles.size()]);
    }
}

From source file:Main.java

/**
 * Convert a collection to an array//from w  w  w . j av  a  2  s  . c  om
 * 
 * @param col
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<T> col, Class<? super T> c) {
    int size = col.size();
    T[] array = (T[]) Array.newInstance(c, size);
    col.toArray(array);

    return array;
}

From source file:eu.planets_project.tb.impl.serialization.ExperimentRecords.java

/**
 * //from  www  .j a  va  2s.c  om
 * @param out
 * @param ecol
 */
public static void writeExperimentsToOutputStream(OutputStream out, Collection<Experiment> ecol) {
    writeExperimentsToOutputStream(out, ecol.toArray(new Experiment[ecol.size()]));
}

From source file:Main.java

public static File[] listFiles(File directory, boolean recurse, FileFilter filter) {
    if (!recurse) {
        return directory.listFiles(filter);
    } else {/*from ww  w . jav  a  2s.co m*/
        Collection<File> mFiles = new java.util.LinkedList<File>();
        innerListFiles(mFiles, directory, filter);
        return (File[]) mFiles.toArray(new File[mFiles.size()]);
    }
}

From source file:Main.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private static Object toObjectArray(Class<?> type, Collection collection) {
    return collection.toArray((Object[]) Array.newInstance(type, collection.size()));
}

From source file:Main.java

/**
 * Returns an array containing all of the elements of the
 * Iterator/*from   w w  w . j  av a2s .co  m*/
 * @param iterator Iterator to copy the contexts of
 * @return an array containing a copy of the iterator contents
 */
public static <T> T[] toArray(Iterator<? extends T> iterator, Class<T> type) {
    if (iterator.hasNext()) {
        Collection arrayList = arrayList(iterator);
        T[] outArray = (T[]) Array.newInstance(type, arrayList.size());

        return (T[]) arrayList.toArray(outArray);
    } else {
        // optimize empty iterator case
        return (T[]) Array.newInstance(type, 0);
    }
}

From source file:Main.java

/**
 * Returns an array containing all of the elements in the given collection. This is a
 * compile-time type-safe alternative to {@link Collection#toArray(Object[])}.
 * /*  www .  ja v  a2  s. c  om*/
 * @param collection the source collection
 * @param clazz the type of the array elements
 * @param <A> the type of the array elements
 * @return an array of type <code>A</code> containing all of the elements in the given
 *         collection
 * 
 * @throws NullPointerException if the specified collection or class is null
 * 
 * @see <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7023484">Sun bug 7023484</a>
 */
public static <A> A[] toArray(Collection<? extends A> collection, Class<A> clazz) {
    Object array = Array.newInstance(clazz, collection.size());
    @SuppressWarnings("unchecked")
    A[] typedArray = collection.toArray((A[]) array);
    return typedArray;
}

From source file:de.kp.ames.web.core.json.JsonUtil.java

/**
 * This is a helper method to convert /*www. j  a  v  a2  s .c om*/
 * a collection of strings into a JSON
 * array
 * 
 * @param c
 * @return
 * @throws Exception
 */
public static JSONArray getJArray(Collection<String> c) throws Exception {

    String[] cs = (String[]) c.toArray(new String[c.size()]);
    return new JSONArray(Arrays.asList(cs));

}

From source file:com.marvelution.hudson.plugins.apiv2.client.services.SearchQuery.java

/**
 * Method to create a {@link IssueSearchQuery} that will search in all the builds of a specific Hudson Job given
 * by name/*from   ww w .  j a  v  a  2s  .  com*/
 * 
 * @param query the query {@link String} {@link Collection} to search for
 * @param jobName the Job name of the specific Hudson job, may be <code>null</code>
 * @return the {@link IssueSearchQuery}
 */
public static IssueSearchQuery createForIssueSearch(Collection<String> query, String jobName) {
    return new IssueSearchQuery(query.toArray(new String[query.size()]), jobName);
}