Java Collection Join joinCollection(Collection a)

Here you can find the source of joinCollection(Collection a)

Description

Generates a comma-separated string consisting of quoted string representations of a collection of objects.

License

Open Source License

Parameter

Parameter Description
a The collection of objects.

Return

The comma-separated string.

Declaration

public static String joinCollection(Collection<String> a) 

Method Source Code

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

import java.util.Collection;

public class Main {
    /**// w w w .  j  av a2 s .c  o  m
     * Generates a comma-separated string consisting of quoted string representations of a collection of objects.
     * @param a The collection of objects.
     * @return The comma-separated string.
     */
    public static String joinCollection(Collection<String> a) {
        return joinCollection(a, ",", true);
    }

    /**
     * Generates a string consisting of the string representations of a collection of objects.
     * @param a The array of objects.
     * @param separator The separator.
     * @param shouldQuote Flag indicating whether or not the string representations should be quoted.
     * @return The string.
     */
    public static String joinCollection(Collection<String> a, String separator, boolean shouldQuote) {
        String result = "";
        String quote = shouldQuote ? "\"" : "";

        if (a != null && a.size() > 0) {
            int i = 0;
            for (Object ai : a) {
                if (i < a.size() - 1)
                    result += quote + ai + quote + separator;
                else
                    result += quote + ai + quote;
                i++;
            }
        }

        return result;
    }
}

Related

  1. joinAndDelimit(final Collection strings, final String sep, final String delim)
  2. joinAsString(Collection collection, String separator)
  3. joinAsStrings(Collection values, String separator)
  4. joinCol(Collection lst, String delim)
  5. joinCollection(Collection collection, char delimiter)
  6. joinCollections(final Collection target, final Collection... collections)
  7. joinCollectionToString(Collection list, String str)
  8. joinCommaDelimitedList(Collection list)
  9. joinEmptyItemIncluded(Collection stringCollection, String delimiter)