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

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

Description

Convert a Collection into a delimited String (e.g.

License

Apache License

Parameter

Parameter Description
coll the Collection to convert
delim the delimiter to use (typically a ",")

Return

the delimited String

Declaration

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

Method Source Code

//package com.java2s;
/*//  ww  w.  j av  a2 s .  c om
 * iBankApp
 *
 * License : Apache License,Version 2.0, January 2004
 *
 * See the LICENSE file in English or LICENSE.zh_CN in chinese
 * in the root directory or <http://www.apache.org/licenses/>.
 *
 * reference from springframework
 */

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

public class Main {
    /**
     * Convert a {@link Collection} to a delimited {@code String} (e.g. CSV).
     * <p>Useful for {@code toString()} implementations.
     * @param coll the {@code Collection} to convert
     * @param delim the delimiter to use (typically a ",")
     * @param prefix the {@code String} to start each element with
     * @param suffix the {@code String} to end each element with
     * @return the delimited {@code 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();
    }

    /**
     * Convert a {@code Collection} into a delimited {@code String} (e.g. CSV).
     * <p>Useful for {@code toString()} implementations.
     * @param coll the {@code Collection} to convert
     * @param delim the delimiter to use (typically a ",")
     * @return the delimited {@code String}
     */
    public static String collectionToDelimitedString(Collection<?> coll,
            String delim) {
        return collectionToDelimitedString(coll, delim, "", "");
    }

    public static boolean isEmpty(String s) {

        return s == null || s.trim().length() == 0;
    }
}

Related

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