Here you can find the source of join(Collection> coll, String delim)
Parameter | Description |
---|---|
coll | the Collection to display |
delim | the delimiter to use (probably a ",") |
public static String join(Collection<?> coll, String delim)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { /**/*from w w w .j a va 2 s . c o m*/ * Convenience method to return a Collection as a delimited (e.g. CSV) * String. E.g. useful for <code>toString()</code> implementations. * @param coll the Collection to display * @param delim the delimiter to use (probably a ",") * @return the delimited String */ public static String join(Collection<?> coll, String delim) { return join(coll, delim, "", ""); } /** * Convenience method to return a Collection as a delimited (e.g. CSV) * String. E.g. useful for <code>toString()</code> 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 join(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(); } }