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

StringcreateTextFromList(Collection list)
create Text From List
if (list.isEmpty())
    return "";
StringBuilder htmlList = new StringBuilder();
for (String listItem : list) {
    htmlList.append("* ").append(listItem);
    htmlList.append("\n");
htmlList.deleteCharAt(htmlList.length() - 1);
...
StringcreateUniqueName(String baseName, Collection names)
Generate a unique name based on the given name and the name of already existing elements.
String uniqueName = baseName;
int idx = -1;
for (String name : names) {
    if (!name.startsWith(baseName)) {
        continue;
    String suffix = name.substring(baseName.length());
    if (suffix.isEmpty()) {
...
CollectionstringToCollection(String string)
Returns a comma-delimitted list of Strings as a Collection.
if (string == null || string.trim().length() == 0) {
    return Collections.emptyList();
Collection<String> collection = new ArrayList<String>();
StringTokenizer tokens = new StringTokenizer(string, ",");
while (tokens.hasMoreTokens()) {
    collection.add(tokens.nextToken().trim());
return collection;
CollectionstringToCollection(String string)
Returns a comma-delimitted list of Strings as a Collection.
if (string == null || string.trim().length() == 0) {
    return Collections.emptyList();
Collection<String> collection = new ArrayList<>();
StringTokenizer tokens = new StringTokenizer(string, ",");
while (tokens.hasMoreTokens()) {
    collection.add(tokens.nextToken().trim());
return collection;
ArrayListstringToCollection(String string, String delim)
This method will return all the values in a delimited String as a Collection of String objects.
return stringToCollection(string, delim, false);
CtoCollection(C c, E... elements)
to Collection
if (elements != null) {
    for (E element : elements) {
        c.add(element);
return c;
CtoCollection(Class cls, Iterable iterable)
to Collection
try {
    C result = cls.newInstance();
    for (T item : iterable) {
        result.add(item);
    return result;
} catch (InstantiationException ex) {
    return null;
...
CollectiontoCollection(Class cls, T[] array)
to Collection
try {
    Collection<T> collection = cls.newInstance();
    for (T t : array) {
        collection.add(t);
    return collection;
} catch (RuntimeException re) {
    throw re;
...
CollectiontoCollection(final char[] charArray)
to Collection
final Collection<String> c = new ArrayList<String>();
for (final Object obj : charArray) {
    c.add(obj.toString());
return c;
CollectiontoCollection(final Iterable iterable)
Converts an Iterable into a Collection.
final Collection<E> col = new ArrayList<E>();
final Iterator<E> it = iterable.iterator();
while (it.hasNext()) {
    final E item = it.next();
    col.add(item);
return col;