Java Utililty Methods Collection Create

List of utility methods to do Collection Create

Description

The list of methods to do Collection Create are organized into topic(s).

Method

CollectiontoCollection(final T[] elements)
Create a collection using an array.
final List<T> result = new ArrayList<T>();
for (T element : elements) {
    result.add(element);
return result;
CollectiontoCollection(Iterable iterable)
to Collection
List<E> collection = new ArrayList<E>();
iterable.forEach(e -> collection.add(e));
return collection;
CollectiontoCollection(Iterable i)
Convert an Iterable to a Collection (ArrayList under the hood).
if (i == null) {
    throw new IllegalArgumentException("Iterable 'i' cannot be null");
Collection<T> c = new ArrayList<T>();
for (T t : i) {
    c.add(t);
return c;
...
CollectiontoCollection(Object o)
to Collection
if (o == null) {
    return Collections.emptyList();
} else {
    return Collections.singletonList(o);
CollectiontoCollection(String value)
to Collection
if (value == null) {
    throw new IllegalArgumentException("Cannot add null value to collection.");
Collection<String> collection = new ArrayList<String>();
collection.add(value);
return collection;