Java Iterable Join join(final Iterable items, final String separator)

Here you can find the source of join(final Iterable items, final String separator)

Description

Pick all elements from the iterable items and convert their string representation into a separator -separated single string.

License

Artistic License

Parameter

Parameter Description
items The source item collection
separator The separator string between items <p/>

Return

A String containing the concatenated string representations of the elements in items .

Declaration

public static String join(final Iterable<?> items, final String separator) 

Method Source Code

//package com.java2s;
/**//from w  w  w .  ja  v  a 2  s  .c o m
 * (c) 2009-2014 Peter Wullinger
 *
 * $Id$
 *
 * Use, modification and restribution of this file are covered by the
 * terms of the Artistic License 2.0.
 *
 * You should have received a copy of the license terms in a file named
 * "LICENSE" together with this software package.
 *
 * Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT
 * HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
 * A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE
 * EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO
 * COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT
 * OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 **/

import java.util.Iterator;

public class Main {
    /**
     * Pick all elements from the iterable {@literal items} and convert their string representation into a
     * {@literal separator}-separated single string.
     *
     * @param items     The source item collection
     * @param separator   The separator string between items
     * <p/>
     * @return A String containing the concatenated string representations of the elements in {@literal items}.
     */
    public static String join(final Iterable<?> items, final String separator) {
        final StringBuilder sequence = new StringBuilder();
        final Iterator<?> iter = items.iterator();
        if (iter.hasNext()) {
            final String first = iter.next().toString();
            sequence.append(first);
            while (iter.hasNext()) {
                sequence.append(separator);
                final String next = iter.next().toString();
                sequence.append(next);
            }
        }
        return sequence.toString();
    }
}

Related

  1. join(final Iterable iterable, final CharSequence separator)
  2. join(final Iterable objects, final CharSequence separator)
  3. join(final Iterable tokens, final String delimiter)
  4. join(final Iterable iterable, final String separator)
  5. join(final Iterable... iterables)
  6. join(final Iterable iterable, final String separator)
  7. join(final Iterable container)
  8. join(final Iterable iterable, final String joinString)
  9. join(final Iterable objs, final String delimiter)