Java Collection Join join(final Collection collection, final String delimiter)

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

Description

A method for joing together a collection of strings using a specified delimiter.

License

Open Source License

Parameter

Parameter Description
collection a parameter
delimiter a parameter

Declaration

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

Method Source Code

//package com.java2s;

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**// ww w  . j av  a 2  s  .  co m
     * A method for joing together a collection of strings using a specified
     * delimiter.
     * 
     * @param collection
     * @param delimiter
     * @return
     */
    public static <T> String join(final Collection<T> collection, final String delimiter) {
        if (collection == null || collection.isEmpty()) {
            return "";
        }

        Iterator<T> it = collection.iterator();
        StringBuffer buffer = new StringBuffer(it.next().toString());

        while (it.hasNext()) {
            buffer.append(delimiter).append(it.next().toString());
        }

        return buffer.toString();
    }
}

Related

  1. join(final Collection strs, final String delimiter)
  2. join(final Collection toJoin)
  3. join(final Collection values)
  4. join(final Collection values, final char separator)
  5. join(final Collection collection)
  6. join(final Collection objs, final String delimiter)
  7. join(final Collection objs, String delimiter)
  8. join(final String d, final Collection c)
  9. join(final String delimiter, final Collection elements)