Java Collection Convert toCommaSeparatedString(final Collection items)

Here you can find the source of toCommaSeparatedString(final Collection items)

Description

Returns string containing item names, each separated from other with comma and space.

License

Open Source License

Parameter

Parameter Description
items a parameter

Declaration

public static String toCommaSeparatedString(final Collection<? extends Object> items) 

Method Source Code


//package com.java2s;
import java.util.*;

public class Main {
    public static final String COMMA_SPACE_SEPARATOR = ", ";

    /**//from  w  ww  .  j  a v  a 2 s.c  om
     * Returns string containing item names, each separated from other with
     * comma and space. Names are obtained via toString() method.
     * 
     * @param items
     * @return
     */
    public static String toCommaSeparatedString(final Collection<? extends Object> items) {
        String result = "";
        for (final Iterator<? extends Object> iter = items.iterator(); iter.hasNext();) {
            result += iter.next();
            if (iter.hasNext()) {
                result += COMMA_SPACE_SEPARATOR;
            }
        }

        return result;
    }
}

Related

  1. toCollectionString(Collection c, char sep)
  2. toCollectionString(Collection collection_p)
  3. toCommaDelimitedString(Collection c)
  4. toCommaDelimitedStringInQuotes(Collection c)
  5. toCommaSep(Collection strings)
  6. toCommaSeparatedValues(Collection list)
  7. toCommaSepared(Collection aListOfString)
  8. toCommaString(Collection c)
  9. toCsvString(Collection collection)