Java Collection Print prettyPrint(Collection c)

Here you can find the source of prettyPrint(Collection c)

Description

Iterates over a collection of objects and joins them together with ", ", for display purposes.

License

BSD License

Parameter

Parameter Description
c The collection, which may be <code>null</code>.

Return

A string representation of the collection's contents.

Declaration

public static final String prettyPrint(Collection<?> c) 

Method Source Code

//package com.java2s;
/*/*from  ww w .  ja  va2  s.  c  om*/
 * 03/16/2014
 *
 * ZScriptUIUtils - Utility methods for the UI.
 * This library is distributed under a modified BSD license.  See the included
 * ZScriptLanguageSupport.License.txt file for details.
 */

import java.util.Collection;

public class Main {
    /**
     * Iterates over a collection of objects and joins them together with
     * <code>", "</code>, for display purposes.
     *
     * @param c The collection, which may be <code>null</code>.
     * @return A string representation of the collection's contents.
     */
    public static final String prettyPrint(Collection<?> c) {

        if (c == null || c.isEmpty()) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        int size = c.size();

        int i = 0;
        for (Object obj : c) {
            sb.append(obj);
            i++;
            if (i < size) {
                sb.append(", ");
            }
        }

        return sb.toString();

    }
}

Related

  1. prettyPrint(Collection input)
  2. prettyPrint(Collection allGroups)
  3. prettyPrintCollection(Collection collection)
  4. prettyPrintListOfClasses( Collection package1DependenciesOnPackage2)