Java Iterable Join join(final Iterable iterable, final CharSequence separator)

Here you can find the source of join(final Iterable iterable, final CharSequence separator)

Description

Joins the string representations of an Iterable collection of objects to one continues string while separating the entities with the provided separator.

License

Open Source License

Parameter

Parameter Description
iterable The Iterable number of objects
separator a parameter

Return

The joined String separated by the given separator

Declaration

public static String join(final Iterable<? extends Object> iterable, final CharSequence separator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007, 2013 compeople AG 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
 *
 * Contributors:/*  w  ww. j av a 2  s  .  c  om*/
 *    compeople AG - initial API and implementation
 *******************************************************************************/

import java.util.Iterator;

public class Main {
    /**
     * Joins the string representations of an {@link Iterable} collection of
     * objects to one continues string while separating the entities with the
     * provided separator.
     * <p>
     * The string representation will be retrieved by String.valueOf()
     * 
     * @param iterable
     *            The {@link Iterable} number of objects
     * @param separator
     * @return The joined String separated by the given separator
     * @since 3.0
     */
    public static String join(final Iterable<? extends Object> iterable, final CharSequence separator) {
        Iterator<? extends Object> oIt;
        if (iterable == null || (!(oIt = iterable.iterator()).hasNext())) {
            return ""; //$NON-NLS-1$
        }
        final StringBuilder sb = new StringBuilder(String.valueOf(oIt.next()));
        while (oIt.hasNext()) {
            sb.append(separator).append(oIt.next());
        }
        return sb.toString();
    }
}

Related

  1. join(CharSequence delimiter, Iterable tokens)
  2. join(CharSequence delimiter, Iterable elements)
  3. join(CharSequence separator, Iterable strings)
  4. join(final CharSequence sep, final Iterable src)
  5. join(final Iterable elements, final String separator)
  6. join(final Iterable objects, final CharSequence separator)
  7. join(final Iterable tokens, final String delimiter)
  8. join(final Iterable iterable, final String separator)
  9. join(final Iterable... iterables)