/** * a new List that contains the specified elements or an empty collection if the elements are null or empty. * * @param elements the elements to put in the list. * @param <T> the type of elements in the collection * @return a new List that contains the specified elements or an empty collection if the elements are null or empty. * @since 1.0.RC3 */ public static <T> List<T> toList(T... elements) { if (elements == null || elements.length == 0) { return java.util.Collections.emptyList(); } // Avoid integer overflow when a large array is passed in int capacity = computeListCapacity(elements.length); ArrayList<T> list = new ArrayList<T>(capacity); java.util.Collections.addAll(list, elements); return list; }
/** * a new List that contains the specified elements or an empty collection if the elements are null or empty. * * @param elements the elements to put in the list. * @param <T> the type of elements in the collection * @return a new List that contains the specified elements or an empty collection if the elements are null or empty. * @since 1.0.RC3 */ public static <T> List<T> toList(Collection<T> elements) { if (elements instanceof List) { return (List<T>) elements; } if (isEmpty(elements)) { return java.util.Collections.emptyList(); } // Avoid integer overflow when a large array is passed in int capacity = computeListCapacity(elements.size()); ArrayList<T> list = new ArrayList<T>(capacity); for (T element : elements) { list.add(element); } return list; }