Java Collection Join join(Collection iterable, String delimiter)

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

Description

join

License

Apache License

Return

A string that contains the items in the given collection with the given delimiter between values. The delimiter will not appear as a prefix to the returned string or as a suffix, it is only used in between two items. The items in the list are turned into strings via . This is simply a helper for calling , passing in null for prefix and suffix.

NOTE: An will be thrown if you pass in null for the collection of items or the delimiter.

Declaration

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

Method Source Code


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

import java.util.Collection;

public class Main {
    /**// ww w  .j  av  a 2  s. c om
     * @return A string that contains the items in the given collection with the given delimiter between values. The
     *          delimiter will not appear as a prefix to the returned string or as a suffix, it is only used in between
     *          two items. The items in the list are turned into strings via {@link String#valueOf(Object)}. This is
     *          simply a helper for calling {@link #join(Collection, String, String, String)}, passing in null for
     *          prefix and suffix.
     *          <p>NOTE: An {@link IllegalArgumentException} will be thrown if you pass in null for the collection of
     *          items or the delimiter.
     */
    public static String join(Collection<?> iterable, String delimiter) {
        return join(iterable, delimiter, null, null);
    }

    /**
     * @return A string that contains the items in the given collection with the given delimiter between values, the
     *          given prefix before any of the items, and the given suffix after all the items. The delimiter will not
     *          appear as a prefix to the returned string or as a suffix, it is only used in between two items. The
     *          items in the list are turned into strings via {@link String#valueOf(Object)}. You can safely pass in
     *          null for the prefix and/or suffix and they will be turned into the empty string, effectively removing
     *          them from the returned string.
     *          <p>NOTE: An {@link IllegalArgumentException} will be thrown if you pass in null for the collection of
     *          items or the delimiter.
     */
    public static String join(Collection<?> iterable, String delimiter, String prefix, String suffix) {
        if (iterable == null)
            throw new IllegalArgumentException("iterable cannot be null");

        if (delimiter == null)
            throw new IllegalArgumentException("delimiter cannot be null");

        if (prefix == null)
            prefix = "";

        if (suffix == null)
            suffix = "";

        StringBuilder sb = new StringBuilder();
        // Add the prefix
        sb.append(prefix);

        // Add each item, with the delimiter between items.
        boolean firstItem = true;
        for (Object obj : iterable) {
            if (!firstItem)
                sb.append(delimiter);
            sb.append(String.valueOf(obj));
            firstItem = false;
        }

        // Add the suffix
        sb.append(suffix);

        return sb.toString();
    }
}

Related

  1. join(Collection collection, String separatorString)
  2. join(Collection elements, String separator)
  3. join(Collection items, char separator)
  4. join(Collection items, String delim)
  5. join(Collection items, String delimiter)
  6. join(Collection objects, String token, StringBuilder buf)
  7. join(Collection objectsToJoin, String separator)
  8. join(Collection objs, String delim)
  9. join(Collection objs, String delim)