Java Collection Join join(Collection collection, String delimiter)

Here you can find the source of join(Collection collection, String delimiter)

Description

Joins the elements of a collection together into one string.

License

Open Source License

Parameter

Parameter Description
collection collection of elements
delimiter string to insert between elements

Return

joined string

Declaration

public static String join(Collection<?> collection, String delimiter) 

Method Source Code


//package com.java2s;
import java.util.Collection;

public class Main {
    /**//  w ww .j a v  a  2 s. com
     * Joins the elements of a collection together into one string.
     *
     * @param collection  collection of elements
     * @param delimiter   string to insert between elements
     * @return joined string
     */
    public static String join(Collection<?> collection, String delimiter) {

        return join(collection, delimiter, "%s");

    }

    /**
     * Joins the elements of a collection together into one string with some
     * format string.
     *
     * @param collection  collection of elements
     * @param delimiter   string to insert between elements
     * @param format      format string for elements
     * @return joined string
     */
    public static String join(Collection<?> collection, String delimiter, String format) {

        StringBuilder str = new StringBuilder();

        for (Object element : collection) {
            if (str.length() > 0)
                str.append(delimiter);
            str.append(String.format(format, element));
        }

        return str.toString();

    }
}

Related

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