Java Collection Join join(String separator, Collection list)

Here you can find the source of join(String separator, Collection list)

Description

Converts a Collection to a separator separated string

License

Open Source License

Parameter

Parameter Description
separator a parameter
list a parameter

Return

separator separated string version of the given list

Declaration

public static String join(String separator, Collection<String> list) 

Method Source Code

//package com.java2s;

import java.util.Collection;

public class Main {
    /**/*from   w w  w .j  a  va 2 s. c o  m*/
     * Converts a {@link Collection} to a <tt>separator<tt> separated string
     *
     * @param separator
     * @param list
     * @return <tt>separator<tt> separated string version of the given list
     */
    public static String join(String separator, Collection<String> list) {
        return join(separator, list, null);
    }

    /**
     * Converts a {@link Collection} to a <tt>separator<tt> separated string.
     * If the <tt>replacement</tt> parameter is not null, it is used to populate
     * the result string instead of list elements.
     *
     * @param separator
     * @param list
     * @param replacement
     * @return <tt>separator<tt> separated string version of the given list
     */
    public static String join(String separator, Collection<String> list, String replacement) {
        StringBuffer sb = new StringBuffer();
        boolean first = true;
        for (final String s : list) {
            if (first)
                first = false;
            else
                sb.append(separator);
            sb.append(replacement == null ? s : replacement);
        }
        return sb.toString();
    }
}

Related

  1. join(String separator, Collection c)
  2. join(String separator, Collection c)
  3. join(String separator, Collection items)
  4. join(String separator, Collection items)
  5. join(String separator, Collection objs)
  6. join(String separator, Collection parts)
  7. join(String separator, Collection strings)
  8. join(String separator, Collection objects)
  9. join(String separator, Collection values)