Java Collection Join join(Collection c, String joinWith)

Here you can find the source of join(Collection c, String joinWith)

Description

Returns a string consisting of all members of a collection separated by the specified string.

License

Apache License

Parameter

Parameter Description
c a collection of objects
joinWith the string that will separate each member of the collection

Declaration

public static String join(Collection<? extends Object> c, String joinWith) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**//www  .j a  v  a 2 s  .  co m
     * Returns a string consisting of all members of a collection separated
     * by the specified string. The <code>toString</code> method of each
     * collection member is called to convert it to a string.
     *
     * @param c a collection of objects
     * @param joinWith the string that will separate each member of the collection
     */
    public static String join(Collection<? extends Object> c, String joinWith) {
        if (c == null)
            return "";

        StringBuilder buf = new StringBuilder();
        boolean first = true;
        for (Object obj : c) {
            if (first)
                first = false;
            else if (joinWith != null)
                buf.append(joinWith);
            buf.append(obj.toString());
        }
        return buf.toString();
    }
}

Related

  1. join(Collection s, String delimiter, boolean doQuote)
  2. join(Collection strings, String sep)
  3. join(Collection strings, String separator)
  4. join(Collection tokens, String separator)
  5. join(Collection var0, Object var1)
  6. join(Collection collection, String joinString)
  7. join(Collection collection, String separator)
  8. join(Collection elements, String separator)
  9. join(Collection list, String separator)