Java Collection Join join(final String separator, final Collection objects)

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

Description

Returns a string of the form elt1.toString() [sep elt2.toString() ...

License

Open Source License

Parameter

Parameter Description
separator the string to use to separate objects
objects a collection of objects. the element order is defined by the iterator over objects
T the type of the objects

Return

a non-null string

Declaration

public static <T> String join(final String separator,
        final Collection<T> objects) 

Method Source Code

//package com.java2s;
/*/*  w  w w  . j  a  v a 2 s.c  o m*/
 * Copyright (c) 2012 The Broad Institute
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Returns a string of the form elt1.toString() [sep elt2.toString() ... sep elt.toString()] for a collection of
     * elti objects (note there's no actual space between sep and the elti elements).  Returns
     * "" if collection is empty.  If collection contains just elt, then returns elt.toString()
     *
     * @param separator the string to use to separate objects
     * @param objects a collection of objects.  the element order is defined by the iterator over objects
     * @param <T> the type of the objects
     * @return a non-null string
     */
    public static <T> String join(final String separator,
            final Collection<T> objects) {
        if (objects.isEmpty()) { // fast path for empty collection
            return "";
        } else {
            final Iterator<T> iter = objects.iterator();
            final T first = iter.next();

            if (!iter.hasNext()) // fast path for singleton collections
                return first.toString();
            else { // full path for 2+ collection that actually need a join
                final StringBuilder ret = new StringBuilder(
                        first.toString());
                while (iter.hasNext()) {
                    ret.append(separator);
                    ret.append(iter.next().toString());
                }
                return ret.toString();
            }
        }
    }
}

Related

  1. join(final String delimiter, final Collection elements)
  2. join(final String s, final Collection c)
  3. join(final String sep, final Collection c)
  4. join(final String sep, final Collection strs)
  5. join(final String separator, final Collection objects)
  6. join(final String separator, final Collection objects)
  7. join(Iterable collection, String delimiter)
  8. join(Iterable collection)
  9. join(String c, Collection d)