Java Collection Concatenate concatElements(final Collection c, final String separator)

Here you can find the source of concatElements(final Collection c, final String separator)

Description

Joins the String representations of the elements of a given Collection using a given separator.

License

Apache License

Parameter

Parameter Description
c the Collection whose elements should be joined
separator the separator to use for joining

Return

the join result as a

Declaration

public static StringBuilder concatElements(final Collection<?> c, final String separator) 

Method Source Code


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

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

public class Main {
    /**/*from  ww w .ja v  a 2s  .c o m*/
     * Joins the {@link String} representations of the elements of a given
     * {@link Collection} using a given separator.
     * 
     * @param c
     *           the {@link Collection} whose elements should be joined
     * @param separator
     *           the separator to use for joining
     * @return the join result as a {@link StringBuilder}
     */
    public static StringBuilder concatElements(final Collection<?> c, final String separator) {
        final StringBuilder builder = new StringBuilder();

        if (c.isEmpty()) {
            return builder;
        }

        final Iterator<?> oIterator = c.iterator();
        builder.append(oIterator.next());

        while (oIterator.hasNext()) {
            builder.append(separator).append(oIterator.next());
        }

        return builder;
    }
}

Related

  1. concat(final Collection... collections)
  2. concat(final Collection... collections)
  3. concat(String separator, Collection args)
  4. concat(String[] first, Collection second)
  5. concatCollection(Collection collection)
  6. concatenate(Collection collections)
  7. concatenate(Collection coll)
  8. concatenate(Collection list)
  9. concatenate(final Collection objects, final String separator)