Java Collection to String toString(Collection collection)

Here you can find the source of toString(Collection collection)

Description

to String

License

Open Source License

Parameter

Parameter Description
collection Collection

Return

String representing given collection

Declaration

public static String toString(Collection<?> collection) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

public class Main {
    /**/* w  w w . j av a2s .c o  m*/
     *
     * @param collection Collection
     * @return String representing given collection
     */
    public static String toString(Collection<?> collection) {
        if (collection == null || collection.isEmpty()) {
            return "";
        }

        StringBuilder stringBuilder = new StringBuilder("[");

        for (Object object : collection) {
            if (object instanceof Collection<?>) {
                stringBuilder.append(toString((Collection<?>) object));
            } else {
                stringBuilder.append(object).append(", ");
            }
        }

        stringBuilder.append("]");

        return stringBuilder.toString().replaceAll(", ]", "]");
    }
}

Related

  1. toString(Collection softwareTags)
  2. toString(Collection collection)
  3. toString(Collection collection, String prefix, String suffix)
  4. toString(Collection c)
  5. toString(Collection c)
  6. toString(Collection collection)
  7. toString(Collection collection)
  8. toString(Collection collection, String separator)
  9. toString(Collection elements, String separator)