code: public static Object toArray(Collection<?> collection, Class<?> componentType) {
      if (componentType.isPrimitive()) {
        Object array = Array.newInstance(componentType, collection.size());
        int index = 0;
        for (Object value : collection) {
          Array.set(array, index++, value);
        }
        return array;
      }
      return collection.toArray((Object[]) Array.newInstance(componentType, collection.size()));
    }
docs: Returns a new array of the given component type (possibly a Java primitive) that is a copy of
  the content of the given collection.
  
  @param collection collection
  @param componentType component type (possibly a Java primitive)
  @return new array