Here you can find the source of toCollection(Class
public static <T, C extends Collection<T>> Collection<T> toCollection(Class<C> cls, T[] array)
//package com.java2s; import java.util.Collection; public class Main { public static <T, C extends Collection<T>> Collection<T> toCollection(Class<C> cls, T[] array) { try {/*from w ww .ja va2 s.c o m*/ Collection<T> collection = cls.newInstance(); for (T t : array) { collection.add(t); } return collection; } catch (RuntimeException re) { throw re; } catch (Throwable t) { throw new IllegalArgumentException(t); } } public static <T> T newInstance(String cls, Class<T> interfaceType) { Class<?> clazz; try { clazz = Class.forName(cls); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("class not found: " + cls, e); } Object obj; try { obj = clazz.newInstance(); } catch (InstantiationException e) { throw new IllegalArgumentException("cannot instantiate class: " + cls, e); } catch (IllegalAccessException e) { throw new IllegalArgumentException("cannot access no-args constructor of class: " + cls, e); } T t; try { t = interfaceType.cast(obj); } catch (ClassCastException e) { throw new IllegalArgumentException( "cannot cast new instance of " + cls + " to interface type " + interfaceType.getName()); } return t; } private static int cast(byte b) { if (b >= 0) { return b; } return 256 + b; } }