Here you can find the source of concatElements(final Collection> c, final String separator)
Parameter | Description |
---|---|
c | the Collection whose elements should be joined |
separator | the separator to use for joining |
public static StringBuilder concatElements(final Collection<?> c, final String separator)
//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; } }