Java Collection Join join(final Collection items, final String glue)

Here you can find the source of join(final Collection items, final String glue)

Description

Implementation of join using a StringBuffer

License

Open Source License

Parameter

Parameter Description
items the Collection of items that will be joined together
glue the string to act as glue in the concatenation

Return

the concatenation of the specified items

Declaration

public static String join(final Collection<? extends Object> items,
        final String glue) 

Method Source Code

//package com.java2s;
/**/*from ww w .j av a  2s  .c  o m*/
 * Copyright (c) 2009 Stephen Evanchik
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Stephen Evanchik - initial implementation
 */

import java.util.Collection;

public class Main {
    /**
     * Implementation of join using a {@link StringBuffer}
     *
     * @param items
     *            the {@link Collection} of items that will be joined together
     * @param glue
     *            the string to act as glue in the concatenation
     * @return the concatenation of the specified items
     */
    public static String join(final Collection<? extends Object> items,
            final String glue) {
        final StringBuffer buffer = new StringBuffer();
        for (final Object o : items) {
            if (buffer.length() > 0) {
                buffer.append(glue);
            }

            buffer.append(o.toString());
        }

        return buffer.toString();
    }
}

Related

  1. join(Collection things, String delim)
  2. join(Collection values, String join)
  3. join(final char _separator, final boolean _quotes, final Collection _list, final String _emptyString)
  4. join(final Collection collection, String delimiter)
  5. join(final Collection e, final String s)
  6. join(final Collection coll, final String delimiter)
  7. join(final Collection coll, final String glue)
  8. join(final Collection items, final String inBetween)
  9. join(final Collection list, final String inbetween)