Java Collection to String collectionToDelimitedString(Collection coll, String delim)

Here you can find the source of collectionToDelimitedString(Collection coll, String delim)

Description

Convenience method to return a Collection as a delimited (e.g.

License

Apache License

Parameter

Parameter Description
coll the Collection to display
delim the delimiter to use (probably a ",")

Return

the delimited String

Declaration

public static String collectionToDelimitedString(Collection<?> coll, String delim) 

Method Source Code


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

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**//from  w  ww.  j  av  a2 s  . c o  m
     * Convenience method to return a Collection as a delimited (e.g. CSV)
     * String. E.g. useful for {@code toString()} implementations.
     * @param coll the Collection to display
     * @param delim the delimiter to use (probably a ",")
     * @return the delimited String
     */
    public static String collectionToDelimitedString(Collection<?> coll, String delim) {
        return collectionToDelimitedString(coll, delim, "", "");
    }

    /**
     * Convenience method to return a Collection as a delimited (e.g. CSV)
     * String. E.g. useful for {@code toString()} implementations.
     * @param coll the Collection to display
     * @param delim the delimiter to use (probably a ",")
     * @param prefix the String to start each element with
     * @param suffix the String to end each element with
     * @return the delimited String
     */
    public static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix,
            String suffix) {
        if (coll == null || coll.isEmpty())
            return "";
        StringBuilder sb = new StringBuilder();
        Iterator<?> it = coll.iterator();
        while (it.hasNext()) {
            sb.append(prefix).append(it.next()).append(suffix);
            if (it.hasNext()) {
                sb.append(delim);
            }
        }
        return sb.toString();
    }
}

Related

  1. collectionToDelimitedString(Collection coll, String delim)
  2. collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix)
  3. collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix)
  4. collectionToDelimitedString(Collection col, String del)
  5. collectionToDelimitedString(Collection coll, String delim)
  6. collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix)
  7. collectionToDelimitedString(Collection values, String delimiter)
  8. collectionToStr(Collection collection)
  9. CollectionToStr(Collection coll)