Here you can find the source of collectionToDelimitedString(Collection> coll, String delim)
Parameter | Description |
---|---|
coll | the Collection to convert |
delim | the delimiter to use (typically a ",") |
public static String collectionToDelimitedString(Collection<?> coll, String delim)
//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; } }