code: public static Collection<Object> newCollectionInstance(Type type) {
      if (type instanceof WildcardType) {
        type = Types.getBound((WildcardType) type);
      }
      if (type instanceof ParameterizedType) {
        type = ((ParameterizedType) type).getRawType();
      }
      Class<?> collectionClass = type instanceof Class<?> ? (Class<?>) type : null;
      if (type == null
          || type instanceof GenericArrayType
          || collectionClass != null
              && (collectionClass.isArray() || collectionClass.isAssignableFrom(ArrayList.class))) {
        return new ArrayList<Object>();
      }
      if (collectionClass == null) {
        throw new IllegalArgumentException("unable to create new instance of type: " + type);
      }
      if (collectionClass.isAssignableFrom(HashSet.class)) {
        return new HashSet<Object>();
      }
      if (collectionClass.isAssignableFrom(TreeSet.class)) {
        return new TreeSet<Object>();
      }
      @SuppressWarnings("unchecked")
      Collection<Object> result = (Collection<Object>) Types.newInstance(collectionClass);
      return result;
    }
docs: Returns a new collection instance for the given type.
  
  <p>Creates a new collection instance specified for the first input collection class that
  matches as follows:
  
  <ul>
  <li>{@code null} or an array or assignable from {@link ArrayList} (like {@link List} or
  {@link Collection} or {@link Object}): returns an {@link ArrayList}
  <li>assignable from {@link HashSet}: returns a {@link HashSet}
  <li>assignable from {@link TreeSet}: returns a {@link TreeSet}
  <li>else: calls {@link Types#newInstance(Class)}
  </ul>
  
  @param type type or {@code null} for {@link ArrayList}.
  @return new collection instance
  @throws ClassCastException if result is does not extend {@link Collection}