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