Skip to content

Instantly share code, notes, and snippets.

@xCASx
Last active December 19, 2015 05:39
Show Gist options
  • Save xCASx/5905279 to your computer and use it in GitHub Desktop.
Save xCASx/5905279 to your computer and use it in GitHub Desktop.
Convert a collection to the array.
/**
* Convert a Collection to the array of {@link Class<T[]>} type
* @param type class of outgoing array
* @param collection collection which should be converted
* @param <T> base type for outgoing array
* @return outgoing array {@link Class<T[]>}
*/
private static <T> T[] convertCollectionToArray(Class<T[]> type, Collection collection) {
T[] t = type.cast(Array.newInstance(type.getComponentType(), collection.size()));
return (T[]) collection.toArray(t);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment