Java Collection Join join(Collection collection, String separator)

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

Description

Joins the elements of the provided Collection into a single String containing the provided elements.

License

Open Source License

Parameter

Parameter Description
collection the <code>Collection</code> of values to join together, may be null
separator the separator character to use, null treated as ""

Return

the joined String, null if null collection input

Declaration

public static String join(Collection collection, String separator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005, 2007 IBM Corporation and others.
 * 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
 *
     //from   ww w. j  av  a2  s .  c  om
 *******************************************************************************/

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

public class Main {
    /**
     * <p>
     * Joins the elements of the provided <code>Collection</code> into a single
     * String containing the provided elements.
     * </p>
     * 
     * <p>
     * No delimiter is added before or after the list. A <code>null</code>
     * separator is the same as an empty String ("").
     * </p>
     * 
     * @param collection
     *            the <code>Collection</code> of values to join together, may be
     *            null
     * @param separator
     *            the separator character to use, null treated as ""
     * @return the joined String, <code>null</code> if null collection input
     */
    public static String join(Collection collection, String separator) {
        // handle null, zero and one elements before building a buffer
        if (collection == null) {
            return null;
        }
        if (collection.isEmpty()) {
            return ""; //$NON-NLS-1$
        }
        final Iterator iterator = collection.iterator();
        final Object first = iterator.next();
        if (!iterator.hasNext()) {
            return first != null ? first.toString() : ""; //$NON-NLS-1$
        }
        // two or more elements
        final StringBuffer buf = new StringBuffer(256);
        if (first != null) {
            buf.append(first);
        }
        while (iterator.hasNext()) {
            if (separator != null) {
                buf.append(separator);
            }
            final Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
        }
        return buf.toString();
    }

    /**
     * <p>
     * Joins the elements of the provided <code>Collection</code> into a single
     * String containing the provided elements.
     * </p>
     * 
     * <p>
     * No delimiter is added before or after the list. A <code>null</code>
     * separator is the same as an empty String ("").
     * </p>
     * 
     * @param collection
     *            the <code>Collection</code> of values to join together, may be
     *            null
     * @param separator
     *            the separator character to use, null treated as ""
     * @return the joined String, <code>null</code> if null collection input
     */
    public static String join(Collection collection, char separator) {
        // handle null, zero and one elements before building a buffer
        if (collection == null) {
            return null;
        }
        if (collection.isEmpty()) {
            return ""; //$NON-NLS-1$
        }
        final Iterator iterator = collection.iterator();
        final Object first = iterator.next();
        if (!iterator.hasNext()) {
            return first != null ? first.toString() : ""; //$NON-NLS-1$
        }
        // two or more elements
        final StringBuffer buf = new StringBuffer(256);
        if (first != null) {
            buf.append(first);
        }
        while (iterator.hasNext()) {
            buf.append(separator);
            final Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
        }
        return buf.toString();
    }
}

Related

  1. join(Collection coll, String separator)
  2. join(Collection collection)
  3. join(Collection collection, String separator)
  4. join(Collection collection, String separator)
  5. join(Collection collection, String separator)
  6. join(Collection collection, String separator, boolean trailing)
  7. join(Collection data)
  8. join(Collection elements, String separator)
  9. join(Collection items)