Java Collection to String toString(final Collection objs)

Here you can find the source of toString(final Collection objs)

Description

Converts an array of objects to a comma separated string

License

Apache License

Parameter

Parameter Description
objs the objects

Return

a comma separated string

Declaration

public static String toString(final Collection<?> objs) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Collection;

public class Main {
    /**/*w  w w  .j  a  v a  2 s  .c om*/
     * Converts an array of objects to a comma separated string
     * 
     * @param objs the objects
     * @return a comma separated string
     */
    public static String toString(final Collection<?> objs) {
        return objs != null ? toString(objs.toArray())
                : toString(new Object[] {});
    }

    /**
     * Converts an array of objects to a comma separated string
     * 
     * @param objs the objects
     * @return a comma separated string
     */
    public static String toString(final Object[] objs) {
        final StringBuilder sb = new StringBuilder();
        sb.append('[');
        if (objs != null) {
            int i = 0;
            for (final Object obj : objs) {
                sb.append(obj.toString());
                if (i < (objs.length - 1)) {
                    sb.append(',');
                }
                i++;
            }
        }
        sb.append(']');
        return sb.toString();
    }
}

Related

  1. toString(Collection collection, String divider)
  2. toString(Collection list, String delimeter)
  3. toString(final Collection collection)
  4. toString(final Collection collection)
  5. toString(final Collection collection, final String separator)
  6. toString(final Collection values)
  7. toString(final Collection list, final String delimiter)
  8. toString(final Collection c)
  9. toString(String[] collection, char separator)