Java Collection Join join(Collection coll, String delim)

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

Description

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

License

Open Source License

Parameter

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

Return

the delimited String

Declaration

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

Method Source Code


//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();
    }
}

Related

  1. join(Collection c, String delimiter)
  2. join(Collection c, String insert)
  3. join(Collection c, String separator)
  4. join(Collection col, String delim)
  5. join(Collection col, String separator)
  6. join(Collection collection, String delimiter)
  7. join(Collection collection, String delimiter)
  8. join(Collection collection, String inputSeparator)
  9. join(Collection collection, String join)