Java Collection Join join(String sep, Collection values)

Here you can find the source of join(String sep, Collection values)

Description

Joins a list of strings (or objects that can be converted to string via Object.toString()) into a single string with fields separated by sep.

License

GNU General Public License

Parameter

Parameter Description
sep the separator
values collection of objects, null is converted to the empty string

Return

null if values is null. The joined string otherwise.

Declaration

public static String join(String sep, Collection<?> values) 

Method Source Code


//package com.java2s;
// License: GPL. For details, see LICENSE file.

import java.util.Collection;

public class Main {
    /**//from  w w w  .ja v  a2  s. com
     * Joins a list of strings (or objects that can be converted to string via
     * Object.toString()) into a single string with fields separated by sep.
     * 
     * @param sep
     *            the separator
     * @param values
     *            collection of objects, null is converted to the empty string
     * @return null if values is null. The joined string otherwise.
     */
    public static String join(String sep, Collection<?> values) {
        if (sep == null)
            throw new IllegalArgumentException();
        if (values == null)
            return null;
        if (values.isEmpty())
            return "";
        StringBuilder s = null;
        for (Object a : values) {
            if (a == null) {
                a = "";
            }
            if (s != null) {
                s.append(sep).append(a.toString());
            } else {
                s = new StringBuilder(a.toString());
            }
        }
        return s.toString();
    }
}

Related

  1. join(String joiner, Collection strings)
  2. join(String sep, Collection objects)
  3. join(String sep, Collection col)
  4. join(String sep, Collection col)
  5. join(String sep, Collection parts)
  6. join(String sep, Collection values)
  7. join(String sep, Collection strings)
  8. join(String sep, Collection strs)
  9. join(String separator, Collection c)