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

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

Description

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

License

Apache License

Parameter

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

Declaration

public static String collectionToDelimitedString(Collection c, 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 w w. j a v a 2 s .  c  o m
     * Convenience method to return a Collection as a delimited (e.g. CSV)
     * String. Useful for toString() implementations.
     * @param c Collection to display
     * @param delim delimiter to use (probably a ",")
     */
    public static String collectionToDelimitedString(Collection c, String delim) {
        if (c == null)
            return "null";
        StringBuffer sb = new StringBuffer();
        Iterator itr = c.iterator();
        int i = 0;
        while (itr.hasNext()) {
            if (i++ > 0)
                sb.append(delim);
            sb.append(itr.next());
        }
        return sb.toString();
    }
}

Related

  1. collectionToCommaSeparatedString(Collection elementCollection)
  2. collectionToCommaSeparatedString(List list)
  3. collectionToCommaString(Collection bundleSelection)
  4. collectionToCSString(Collection col)
  5. collectionToDelimitedString( Iterable iterable)
  6. collectionToDelimitedString(Collection coll, String delim)
  7. collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix)
  8. collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix)
  9. collectionToDelimitedString(Collection col, String del)