Java Collection Join join(Iterable collection, String delimiter)

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

Description

Concatenates the elements from the collection to one string.

License

Open Source License

Parameter

Parameter Description
collection collection
delimiter delimiter between the elements

Return

concatenation

Declaration

public static String join(Iterable collection, String delimiter) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;

public class Main {
    /**/*from  w w w. ja v a  2 s  .c om*/
     * Concatenates the elements from the collection to one string.
     * If the collection is empty an empty string is returned.
     *
     * @param collection collection
     * @param delimiter delimiter between the elements
     * @return concatenation
     */
    public static String join(Iterable collection, String delimiter) {
        return join(collection.iterator(), delimiter);
    }

    /**
     * Concatenates the elements of the iterator to one string.
     * If the iterator has no elements an empty string is returned.
     *
     * @param iterator iterator
     * @param delimiter delimiter between the elements
     * @return concatenation
     */
    public static String join(Iterator iterator, String delimiter) {
        return join(iterator, "", delimiter, "");
    }

    /**
     * Concatenates the elements from the collection to one string.
     * If the collection is empty an empty string is returned.
     *
     * @param collection collection
     * @param start start string
     * @param delimiter delimiter between the elements
     * @param end end string
     * @return concatenation
     */
    public static String join(Iterable collection, String start, String delimiter, String end) {
        return join(collection.iterator(), start, delimiter, end);
    }

    /**
     * Concatenates the elements of the iterator to one string.
     * If the iterator has no elements an empty string is returned.
     *
     * @param iterator iterator
     * @param start start string
     * @param delimiter delimiter between the elements
     * @param end end string
     * @return concatenation
     */
    public static String join(Iterator iterator, String start, String delimiter, String end) {
        if (iterator.hasNext()) {
            StringBuffer s = new StringBuffer();

            s.append(start);
            s.append(iterator.next());

            while (iterator.hasNext()) {
                s.append(delimiter);
                s.append(iterator.next());
            }

            s.append(end);

            return s.toString();
        } else {
            return "";
        }
    }
}

Related

  1. join(final String sep, final Collection c)
  2. join(final String sep, final Collection strs)
  3. join(final String separator, final Collection objects)
  4. join(final String separator, final Collection objects)
  5. join(final String separator, final Collection objects)
  6. join(Iterable collection)
  7. join(String c, Collection d)
  8. join(String delim, Collection list)
  9. join(String delim, Collection col)